Jack
Jack

Reputation: 182

How to deserialize multidimensional JSON

I know people asked and already got some answers very similar question before like this, but still, I couldn't figure it out about mine. I have a JSON file contains a multidimensional object, like below:

{
      "Common": {
        "Required": "Required Entry ",
        "Photos": "Photos",
        "Videos": "Videos",
        "Register": "Register"
      },
      "Forms": {
        "Form": "Forms",
        "Name": "Name",
        "Phone": "Phone",
        "Email": "Email",
        "Message": "Message"
      },
      "Sections": {
        "Home": {
          "EventDateTime": "",
          "MainTitle": "",
          "SubTitle": ""
        },
        "About": {},
        "Venue": {},
        "Schedule": {},
        "Speakers": {},
        "Sponsors": {},
        "Price": {},
        "Contact": {}
      }
    }

I would like to deserialize it into my view model (LanguagesViewModel) like this:

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class LanguagesViewModel
{
    public Common Common { get; set; }
    public Buttons Buttons { get; set; }
    public Forms Forms { get; set; }
    public Navbar Navbar { get; set; }
    public Sections Sections { get; set; }
}

public class Common
{
    public string Required { get; set; }
    public string Photos { get; set; }
    public string Videos { get; set; }
    public string Register { get; set; }
}

public class Forms
{
    public string Form { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Message { get; set; }
}

public class Sections
{
    public Home Home { get; set; }
    public About About { get; set; }
    public Venue Venue { get; set; }
    public Schedule Schedule { get; set; }
    public Speakers Speakers { get; set; }
    public Sponsors Sponsors { get; set; }
    public Price Price { get; set; }
    public Contact Contact { get; set; }
}

public class Home
{
    public string EventDateTime { get; set; }
    public string MainTitle { get; set; }
    public string SubTitle { get; set; }
}

public class About
{

}

public class Venue
{

}

public class Schedule
{

}

public class Speakers
{

}

public class Sponsors
{

}

public class Price
{

}

public class Contact
{

}

}

Some of the snippet to do this:

using (StreamReader sr = new StreamReader(language_file_path))
{
    string contents = sr.ReadToEnd();
    items = JsonConvert.DeserializeObject<LanguagesViewModel>(contents);
}

Somehow, I only can get the first level of the objects, which is:

LanguagesViewModel{
    Common:null,
    Forms:null,
    Sections:null
}

Not the second level, not the third level. Did I do something wrong or have I missed something? Very appreciated for any kind of help.

Thank you.

Upvotes: 1

Views: 131

Answers (1)

Shervin Ivari
Shervin Ivari

Reputation: 2551

You can Use this static class

public static class JsonHelper 
{
    public static T ToObject<T>(this string content)
    {
        var obj = JObject.Parse(content).GetValue(typeof(T).Name);

        if (obj == null)
            throw new NullReferenceException();
        else
            return obj.ToObject<T>();
        //This ToObject here is default method written in object
    }
}

Usage

var mymodel= json.ToObject<Forms>();

Or create a JSON object and read it with magic strings.

//Creating your JSON object
JObject content = JObject.Parse(sr.ReadToEnd()//or your json);

//content["your object name"] let you access to you object
var common =(Common)content["Common"];

in multidimensional objects, you can access them like this.

 //content["level1"]["level2"]["level3"] & ...
 var sections= (Home)content["Sections"]["Home"];

Also this way may work but i prefer the way with magic strings.

dynamic jsonObject = new JObject.Parse(sr.ReadToEnd());
var common = jsonObject.Common;

You can find more in this link

I hope this Helps!

Upvotes: 2

Related Questions