0

I'm using JavaScriptSerializer to deserialize a json into a class I made but I have a problem.

the json I'm trying to serialize is in this form. The form of the json is not in my control.

      {"list": {
        "p1": {
          "a": 6,
          "b": 7
        },
        "p2": {
          "a": 1,
          "b": 1
        }
       }

I'm trying to deserialize this into a class like this;

public class jsonObject 
{
    public listType list {get;set;}

    internal class listType 
    { 

    }

}

and my deserialize method;

var engine = new JavaScriptSerializer();
dynamic foo = engine.Deserialize<jsonObject>(json);

What I can't figure out what to do with the items inside the list. It's not an array and the number of objects inside it can vary. It would sometimes be just p1, and it would sometimes go until p7. Is there a solution to this other than declaring p1,p2,p3,p4,p5,p6,p7 inside my listType?

4 Answers 4

1

your JSON string seems not correct. try to use this:

{"list": [
    "p1": {
      "a": 6,
      "b": 7
    },
    "p2": {
      "a": 1,
      "b": 1
    }]
}

I've added an opening brackets for the list in order to mark it as an object and changed the array opening brackets to square brackets.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

var foo = engine.Deserialize<List<jsonObject>>(json);

Comments

0

Okay so I've found something that works;

I've made another class, lets call it childType

public childType
{
   public int a {get;set;}
   public int b {get;set;}
}

then I declared listType as

 public Dictionary<string,childType> list {get;set;}

the key of the dictionary turned out to be the name of the child object in the json (p1,p2,p3,p4) and the value got assigned to a childType.

Comments

0

The format of json you get must be escaped properly. They you can use Json.Net like so to parse it.

JObject obj = JObject.Parse(@"
     {""list"": {
        ""p1"": {
          ""a"": 6,
          ""b"": 7
        },
        ""p2"": {
          ""a"": 1,
          ""b"": 1
        }
       }}
    ");

You said you are not sure what the names of properties inside list could be. So i will use Children() method of a JProperty

//In case you are not sure if `a` or `b` will also change their name go for this using an anonymous type

        var result =    obj.Property("list")
    .Children()
    .Children()
    .Children()
    .Select(x=> new {a =x.Children().ElementAt(0).ToString(), b = x.Children().ElementAt(1).ToString()});

//In case you are sure about `a` and `b` go for this using an anonymous type

        var result =    obj.Property("list")
    .Children()
    .Children()
    .Children()
    .Select(x=> new {a =x["a"].ToString(), b = x["b"].ToString()});

//In case you need a concrete type to store your data,i am using your class `childType`

        public class childType
        {
           public int a {get;set;}
           public int b {get;set;}
        }

            List<childType> lst = obj.Property("list")
    .Children()
    .Children()
    .Children()
    .Select(x=> new childType {a =Convert.ToInt32(x["a"]), b = Convert.ToInt32(x["b"])}).ToList();

I came up with this solution with limited knowledge of JObject. It needs to be improved.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.