0

this is the JSON I have

{
 "productNum":6,
 "01":
{"US_7":"pna886377847444","US_7_5":"pna886377847529","US_8":"pna886377847604","US_8_5":"pna886377847666","US_9":"pna886377847741","US_9_5":"pna886377847826","US_10":"pna886377847895","US_10_5":"pna886377847987","US_11":"pna886377848069","US_11_5":"pna886377848144","US_12":"pna886377848229","US_13":"pna886377848328","US_14":"pna886377848427"},

 "02":
{"US_7":"pna886377849103","US_7_5":"pna886377849202","US_8":"pna886377849295","US_8_5":"pna886377849394","US_9":"pna886377849493","US_9_5":"pna886377849592","US_10":"pna886377849660","US_10_5":"pna886377849745","US_11":"pna886377849820","US_11_5":"pna886377849905","US_12":"pna886377849981","US_13":"pna886377850086","US_14":"pna886377850185"}
}

A better view of the data:

Better view

What class should I describe to parse it ?

4 Answers 4

2

Looks like you can't turn this into a class because 02 is not a valid property name.

You can try using JObject that comes with Json.Net. It acts much like a Dictionary:

Here's a sample:

[Test]
public void Parse()
{
    const string src = @"{
    ""productNum"":6,
    ""01"":
    {""US_7"":""pna886377847444"",""US_7_5"":""pna886377847529"",""US_8"":""pna886377847604"",""US_8_5"":""pna886377847666"",""US_9"":""pna886377847741"",""US_9_5"":""p    na886377847826"",""US_10"":""pna886377847895"",""US_10_5"":""pna886377847987"",""US_11"":""pna886377848069"",""US_11_5"":""pna886377848144"",""US_12"":""pna88637784    8229"",""US_13"":""pna886377848328"",""US_14"":""pna886377848427""},

    ""02"":
    {""US_7"":""pna886377849103"",""US_7_5"":""pna886377849202"",""US_8"":""pna886377849295"",""US_8_5"":""pna886377849394"",""US_9"":""pna886377849493"",""US_9_5"":""p    na886377849592"",""US_10"":""pna886377849660"",""US_10_5"":""pna886377849745"",""US_11"":""pna886377849820"",""US_11_5"":""pna886377849905"",""US_12"":""pna88637784    9981"",""US_13"":""pna886377850086"",""US_14"":""pna886377850185""}
    }";

    // filtering out the "productNum:6"
    var dest =
        JsonConvert.DeserializeObject<IDictionary<string, object>>(src)
        .Where(x => x.Value.GetType() == typeof (JObject));


    foreach (var item in dest)
    {
        var obj = (JObject) item.Value;

        Console.WriteLine(item.Key);

        foreach (var d in obj)
        {
            Console.WriteLine("{0}: {1}", d.Key, d.Value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use json.Net - http://james.newtonking.com/pages/json-net.aspx

Beyond that your question is too general. There are many ways you can go about doing it. The simple approach is to make a class, call it Product it's definition would something like;

 public class Product
 {
     int productNum;
     InnerData one;
     InnerData two;
 }

Before you serialize, rewrite the 01 and 02 to be one and two. InnerData should look something like;

 public class InnerData
 {
     string US_1;
     string US_2;
     // rest of US_x fields
 }

Then you can use the deserialize method - http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm

 Product prod1 = jsonConvert.Deserialize<Product>(jsonString);

Comments

1

Found a solution myself:

string jString = File.ReadAllText(@"C:\_junk\funkyJSON.txt");
var deserializer = new JavaScriptSerializer();
var result = deserializer.DeserializeObject(jString);
var mapDyn = result as Dictionary<string, object>;
var valueSize = ((Dictionary<string, object>)mapDyn["01"])["US_7"].ToString();

Comments

1

Using .Net 4.5's DataContractSerializer, you can give a JSON element any variable name, while specifying its actual name with the "Name" attribute.

So your class could look like:

[DataContract]
public class MyData
{
     [DataMember(Name="01")]
     string Var1;

     ...
}

http://msdn.microsoft.com/en-us/library/bb412179.aspx

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.