1

I have JSON returning in the following format:

{
"Items": [
    {
        "unique_id": "11111111111",
        "rages": {
            "rage_content": "Hello rage 2",
            "date_stamp": "21/07/2017",
            "id": 2
        }
    },
    {
        "unique_id": "2222222222",
        "rages": {
            "rage_content": "Hello rage 1",
            "date_stamp": "21/07/2017",
            "id": 1
        }
    }
],
"Count": 2,
"ScannedCount": 2
}

And I have the following 2 classes defined:

Items.cs:

namespace ragevent_A0._0._1
{
    class Items
    {
        public String rage_id { get; set; }
        public rage rage { get; set; }

    }
}

rage.cs:

class rage
{
    public String rage_content { get; set; }
    public String date_stamp { get; set; }
    public int id { get; set; }
}

I am using the following code in order to attempt to deseralize the JSON returned above:

List<Items> data = JsonConvert.DeserializeObject<List<Items>>(json);

However, I am not able to successfully deserialize the data due to the above error. I have tried a few solutions online, however I have not managed to find a solution which works with the format of my returned JSON. I have used a JSON formatter and it is formatted correctly, so that shouldn't be the issue.

Any help would be much appreciated!

6
  • 2
    Try json2csharp.com to build the class structure Commented Jul 21, 2017 at 14:22
  • your c# class has rage_id while your JSON object has "unique_id". Is that no problem? Also you are not deserializing an array of objects but and object containing that array Commented Jul 21, 2017 at 14:24
  • @danielspaniol No, that will not raise any exception Commented Jul 21, 2017 at 14:25
  • Another nice VS extension is marketplace.visualstudio.com/items?itemName=DangKhuong.JSONtoC Commented Jul 21, 2017 at 14:30
  • @Sir Rufo json2csharp sorted me out, thank you! Commented Nov 29, 2018 at 18:49

1 Answer 1

2

For the posted JSON data below should be the model you need (credit: http://json2csharp.com/). There is mismatch between the property name rage_id. You can use JsonProperty attribute

public class Rages
{
    public string rage_content { get; set; }
    public string date_stamp { get; set; }
    public int id { get; set; }
}

public class Item
{
    [JsonProperty(Name="rage_id")]
    public string unique_id { get; set; }
    public Rages rages { get; set; }
}

public class RootObject
{
    public List<Item> Items { get; set; }
    public int Count { get; set; }
    public int ScannedCount { get; set; }
}

Your deserialization should be

var data = JsonConvert.DeserializeObject<RootObject>(json); 
Sign up to request clarification or add additional context in comments.

13 Comments

Ah ok awesome, cheers. I have added this into my application however I am still getting an error when trying to deseralize. Sorry about the unique_id - it was a typo. I am using this to deseralize: List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(json);
@CallumHolden The json document describes a single object and not an array. An array would start with [ and an object start with { - do you see?
@CallumHolden, you don't have an array of root object. see edit in answer.
@CallumHolden I have the strange feeling you did not get the whole point. Anyway, you should have a working solution for this ;o)
@SirRufo, was about to write the same ... so +1 :)
|

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.