Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET deserialization

I'm new with JSON.NET and I'm trying to deserialize a JSON string to a simple .NET object. Here is a snippet of my code:

public void DeserializeFeed(string feed)
{
    JsonSerializer ser = new JsonSerializer();
    Post deserializedPost = JsonConvert.DeserializeObject<Post>(feed);

    if (deserializedPost == null)
        MessageBox.Show("JSON ERROR !");
    else
    {
        MessageBox.Show(deserializedPost.titre);
    }
}

When I do

MessageBox.Show(deserializedPost.titre);

I always get this error:

Value can not be null.

Here is my object that I want to fill with the retrieved JSON element:

using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MeltyGeeks
{
    public class Post
    {
        public String titre { get; set; }
        public String aresum { get; set; }

        // Constructor
        public Post()
        {
        }
    }
}

And here is a snippet of my JSON string:

{"root_tab":{"tab_actu_fil":{"data":[{"c_origine":"MyApp",
"titre":"title of first article",
"aresum":"this is my first Article
"tab_medias":{"order":{"810710":{"id_media":810710,"type":"article","format":"image","height":138,"width":300,"status":null}}}},
like image 256
Nizar B. Avatar asked Mar 25 '12 12:03

Nizar B.


2 Answers

The JSON structure you have shown doesn't match the Post object. You could define additional classes to represent this structure:

public class Root
{
    public RootTab root_tab { get; set; }
}

public class RootTab
{
    public ActuFil tab_actu_fil { get; set; }
}

public class ActuFil
{
    public Post[] Data { get; set; }
}

public class Post
{
    public String titre { get; set; }
    public String aresum { get; set; }
}

and then:

string feed = ...
Root root = JsonConvert.DeserializeObject<Root>(feed);
Post post = root.root_tab.tab_actu_fil.Data[0];

or if you don't want to define those additional objects you could do this:

var root = JsonConvert.DeserializeObject<JObject>(feed);
Post[] posts = root["root_tab"]["tab_actu_fil"]["data"].ToObject<Post[]>();
string titre = posts[0].titre;
like image 154
Darin Dimitrov Avatar answered Oct 13 '22 23:10

Darin Dimitrov


The json string you posted contains much more than a single Post. To deserialize it, you have to design classes for all the objects contained in the json string, so that you can navigate through the data by accessing properties.

These classes could be as following:

class Feed {
    public RootTab root_tab { get; set; }
}

class RootTab {
    public TabActuFil tab_actu_fil {get; set; }
}

class TabActuFil {
    public List<Post> data { get; set; }
}

class Post {
    public string c_origine {get; set; }
    public string titre {get; set; }
}
like image 44
Yogu Avatar answered Oct 14 '22 00:10

Yogu