-2

I have a block of JSON as follows:

{
    "FirstName": "JON",
    "LastName": "BAYN",
    "Data": [
        {
            "Plan": "DAY"
        }
    ]
}

I have built it using JavaScriptSerializer like

JavaScriptSerializer serializer_user = new JavaScriptSerializer();
                            dynamic jsonObject = serializer_user.Deserialize<dynamic>(content_);

dynamic firstname = jsonObject["FirstName"];
firstname = jsonObject["FirstName"];

But I am not able to read from nested "Details" >> "Plan". I've been unable to piece together how to accomplish this goal.

5
  • "But I am not able to read from nested Details >> Plan" - do you try it? Show us. Commented Apr 18, 2019 at 3:55
  • Can you retrieve the FirstName and LastName? Did you create a class for Deserialization? Commented Apr 18, 2019 at 3:55
  • Possible duplicate of Deserializing JSON data to C# using JSON.NET Commented Apr 18, 2019 at 4:21
  • @NatPongjardenlarp it is not a duplicate because it is about JavaScriptSerializer and not JSON.Net Commented Apr 18, 2019 at 5:50
  • Hazz_Rush - Can you retrieve the FirstName and LastName? -- yes with this dynamic firstname = jsonObject["FirstName"]; firstname = jsonObject["FirstName"]; Commented Apr 18, 2019 at 11:03

2 Answers 2

2

At first, make model class to your json schema:

public class Rootobject
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Gender { get; set; }
    public int MemberID { get; set; }
    public Detail[] Details { get; set; }
}

public class Detail
{
    public string Plan { get; set; }
    public string Product { get; set; }
    public DateTime ProductStartDate { get; set; }
    public DateTime ProductEndDate { get; set; }
    public string Flag { get; set; }
}

Now you can deserialize your json string to RootObject (Use Json.NET instead of JavaScriptSerializer because it is faster etc):

using Newtonsoft.Json;
..
// If Json.NET is not option:
// var obj = new JavaScriptSerializer().Deserialize<Rootobject>(json)
var obj = JsonConvert.DeserializeObject<Rootobject>(json);

And now you are able to access object structure like following:

if (obj.Details != null)
{
    foreach (var detail in obj.Details)
    {
        Console.WriteLine(detail.Plan);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well, this will actually solve the problem, but what if OP can't switch from JavaScriptSerializer to JSON.net? Can you add a solution for that case?
Risto M - Perfect!! if "Details" array more than 1 or none.. in that case getting error as index was outside the bounds of the json array
@Vivan_J Yep, that obj.Details[0] was just an example. You have check array bounds before accessing Details-array
@RistoM obj.Details[0].plan.Length > 1 && obj.Details[0].plan != null not working if it none..
I updated answer to be more resilient (rememer to check answer accepted if working :)
0

If you don't want to create new classes for it and deserialize it, you could just do a regex.

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.