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;
demongolem
  • 9,474
  • 36
  • 90
  • 105
user2282587
  • 83
  • 1
  • 1
  • 3
  • 2
    It is not clear what your actual problem is yet. What is `DATACLASS`? – 48klocs Apr 15 '13 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") – user2282587 Apr 15 '13 at 16:14
  • I was talking about RootObject - not Data! Data defines the elements, RootObject the list itself. – user2282587 Apr 15 '13 at 16:17

1 Answers1

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.

cgotberg
  • 2,045
  • 1
  • 18
  • 18
  • 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; – user2282587 Apr 15 '13 at 16:48
  • The code above wouldn't work out the the box as soon as it hits var deserializedItemsFromRoot = JsonConvert.DeserializeObject>(rootSerialized); it's going to throw an exception. – cgotberg Apr 15 '13 at 17:20
  • 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. – cgotberg Apr 15 '13 at 17:21
  • Thank you so much cgotberg. It's finally working! Your "ToList" - Hint made it. – user2282587 Apr 15 '13 at 17:29