-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.

Vivan_J
  • 3
  • 6
  • _"But I am not able to read from nested Details >> Plan"_ - do you try it? Show us. – vasily.sib Apr 18 '19 at 03:55
  • Can you retrieve the `FirstName` and `LastName`? Did you create a class for Deserialization? – HasithaJay Apr 18 '19 at 03:55
  • Possible duplicate of [Deserializing JSON data to C# using JSON.NET](https://stackoverflow.com/questions/2546138/deserializing-json-data-to-c-sharp-using-json-net) – Ňɏssa Pøngjǣrdenlarp Apr 18 '19 at 04:21
  • @NatPongjardenlarp it is not a duplicate because it is about `JavaScriptSerializer` and not `JSON.Net` – vasily.sib Apr 18 '19 at 05:50
  • Hazz_Rush - Can you retrieve the FirstName and LastName? -- yes with this dynamic firstname = jsonObject["FirstName"]; firstname = jsonObject["FirstName"]; – Vivan_J Apr 18 '19 at 11:03

2 Answers2

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);
    }
}
Risto M
  • 2,919
  • 1
  • 14
  • 27
  • 1
    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? – vasily.sib Apr 18 '19 at 06:29
  • 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 Apr 18 '19 at 11:00
  • @Vivan_J Yep, that `obj.Details[0]` was just an example. You have check array bounds before accessing Details-array – Risto M Apr 18 '19 at 11:04
  • @RistoM obj.Details[0].plan.Length > 1 && obj.Details[0].plan != null not working if it none.. – Vivan_J Apr 18 '19 at 11:14
  • I updated answer to be more resilient (rememer to check answer accepted if working :) – Risto M Apr 18 '19 at 11:32
0

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