7

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}}}},
3
  • 1
    The json you posted does not match the Post class against which you deserialize. Commented Mar 25, 2012 at 12:37
  • Thanks Yogu for your answer but can you give me more accurate information about my problem ? I'm completely noob with JSON.NET. Commented Mar 25, 2012 at 12:38
  • See Darin Dimitrov's or my answer. Commented Mar 25, 2012 at 12:44

2 Answers 2

10

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;
Sign up to request clarification or add additional context in comments.

1 Comment

To be more accurate, I'm new with C# and I want a collection of object with my list root element.
2

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; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.