0

I know how to deserialize basic Json object. I am having a problem with nested objects; for example, here is an example json i want to deserialize.

{
    "data": {
        "A": {
            "id": 24,
            "key": "key",
            "name": "name",
            "title": "title"
        },
        "B": {
            "id": 37,
            "key": "key",
            "name": "name",
            "title": "title"
        },
        "C": {
            "id": 18,
            "key": "key",
            "name": "name",
            "title": "title"
        },
        "D": {
            "id": 110,
            "key": "key",
            "name": "name",
            "title": "title"
        }
      },
    "type": "type",
    "version": "1.0.0"
}

Now "data" has an unknown number of objects, could be 100 could be 1000 or could be just 1 and all of them with different name. My ultimate goal is to get the information of each object inside data.

I tried basic json but didn't work at all.

Anyways, this is what I have tried...

I made class called data

public class data
{
    public long id { get; set; }
    public string key { get; set; }
    public string name { get; set; }
    public string title { get; set; }
}

then I made another class called test

public class test
{
    /*
    I have also tried this, which works but then I don't know what to do with it and how to deserialize the information of it.
    //public Newtonsoft.Json.Linq.JContainer data { get; set; }
    */
    public List<List<data>> data { get; set; }
    public string type { get; set; }
    public string version { get; set; }
}

and in my driver application I did this

 string downloadedData = w.DownloadString(link);
 test t = JsonConvert.DeserializeObject<test>(downloadedData);

But this didn't work as I expected to. Any help would be appreciated.

5
  • In the test class how about public Dictionary<string, data> data. Instead of List<List<data>> Commented Nov 8, 2017 at 19:28
  • You can generate JSON-classes from JSON code from within visual studio. Select your JSON code, copy it to the clipboard, then go to EDIT -> PASTE SPECIAL -> JSON TO CLASSES. Then you can use the generated classes for deserialization. Commented Nov 8, 2017 at 19:47
  • @Michael while that's a very good tip it will not work for this example. Commented Nov 8, 2017 at 20:05
  • @SamMarion Thank you! I got it to work using Dictionary. Commented Nov 8, 2017 at 20:11
  • @Michael Thanks for the tip, I took a look into it as well, its great but like SamMarion said, it's not what I was looking for in this example, but thank you! Commented Nov 8, 2017 at 20:12

1 Answer 1

6

You are looking for a dictionary.

Use this as your class definition:

public class Rootobject
    {
        public Dictionary<string, DataObject> data { get; set; }
        public string type { get; set; }
        public string version { get; set; }
    }
    public class DataObject
    {
        public int id { get; set; }
        public string key { get; set; }
        public string name { get; set; }
        public string title { get; set; }
    }

And this demonstrates that reading your object works:

var vals = @"{

""data"": {
    ""A"": {
        ""id"": 24,
        ""key"": ""key"",
        ""name"": ""name"",
        ""title"": ""title""
    },
    ""B"": {
        ""id"": 37,
        ""key"": ""key"",
        ""name"": ""name"",
        ""title"": ""title""
    },
    ""C"": {
        ""id"": 18,
        ""key"": ""key"",
        ""name"": ""name"",
        ""title"": ""title""
    },
    ""D"": {
        ""id"": 110,
        ""key"": ""key"",
        ""name"": ""name"",
        ""title"": ""title""
    }
  },
""type"": ""type"",
""version"": ""1.0.0""
}";
var obj = JsonConvert.DeserializeObject<Rootobject>(vals);
Sign up to request clarification or add additional context in comments.

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.