8

I'm trying to deserialize a given JSON file in C# using a tutorial by Bill Reiss. For XML data in a non-list this method works pretty well, though I would like to deserialize a JSON file with the following structure:

public class Data
{
    public string Att1 { get; set; }
    public string Att2 { get; set; }
    public string Att3 { get; set; }
    public string Att4 { get; set; }
}

public class RootObject
{

public List<Data> Listname { get; set; }
}

My problem is with using JSON.Net's ability to create / put data into lists, and then displaying the list on an XAML page. My idea so far (which is not working):

var resp = await client.DoRequestJsonAsync<DATACLASS>("URL");
string t = resp.ToString();
var _result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<DATACLASS>>(t);
XAMLELEMENT.ItemsSource = _result;
3
  • 2
    It is not clear what your actual problem is yet. What is DATACLASS? Commented Apr 15, 2013 at 16:05
  • I'm sorry! The Problem is that my code is not working (Debugger.Break()). DATACLASS is the "class Data" described above. (I'm sorry for this confusing Name. You can take it as "Data") Commented Apr 15, 2013 at 16:14
  • I was talking about RootObject - not Data! Data defines the elements, RootObject the list itself. Commented Apr 15, 2013 at 16:17

1 Answer 1

22

So I think you're probably trying to deserialize to the wrong type. If you serialized it to RootObject and try to deserialize to List it's going to fail.

See this example code

public void TestMethod1()
    {
        var items = new List<Item>
                        {
                            new Item { Att1 = "ABC", Att2 = "123" }, 
                            new Item { Att1 = "EFG", Att2 = "456" }, 
                            new Item { Att1 = "HIJ", Att2 = "789" }
                        };
        var root = new Root() { Items = items };
        var itemsSerialized = JsonConvert.SerializeObject(items);
        var rootSerialized = JsonConvert.SerializeObject(root);

        //This works
        var deserializedItemsFromItems = JsonConvert.DeserializeObject<List<Item>>(itemsSerialized); 

        //This works
        var deserializedRootFromRoot = JsonConvert.DeserializeObject<Root>(rootSerialized); 

        //This will fail.  YOu serialized it as root and tried to deserialize as List<Item>
        var deserializedItemsFromRoot = JsonConvert.DeserializeObject<List<Item>>(rootSerialized);

        //This will fail also for the same reason 
        var deserializedRootFromItems = JsonConvert.DeserializeObject<Root>(itemsSerialized);
    }

class Root
{
    public IEnumerable<Item> Items { get; set; } 
}

class Item
{
    public string Att1 { get; set; }
    public string Att2 { get; set; }
}

Edit: Added complete code.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried your code. I added the following lines in order to Display it. Do you see the mistake (it's not working. No Error just no change)? string t = deserializedItemsFromItems.ToString(); PhoneList.ItemsSource = t; string v = deserializedRootFromRoot.ToString(); PhoneList.ItemsSource = v;
The code above wouldn't work out the the box as soon as it hits var deserializedItemsFromRoot = JsonConvert.DeserializeObject<List<Item>>(rootSerialized); it's going to throw an exception.
Are you having a problem with the deserialization or assigning ItemsSource? Assuming you get an deserialized object back then something.ItemsSource = Root.Items.ToList(); should work.
Thank you so much cgotberg. It's finally working! Your "ToList" - Hint made it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.