使用名称为数字的动态对象列表解析JSON

时间:2013-05-01 19:47:56

标签: c# .net json

这是我的JSON

{
 "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"}
}

更好地查看数据:

Better view

我应该描述什么类来解析它?

4 个答案:

答案 0 :(得分:2)

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

除此之外,你的问题太笼统了。你可以通过很多方式去做。简单的方法是创建一个类,称之为Product,它的定义就像;

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

在序列化之前,请将0102重写为onetwoInnerData应该看起来像;

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

然后您可以使用反序列化方法 - http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm

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

答案 1 :(得分:2)

看起来您无法将其转换为类,因为02不是有效的属性名称。

您可以尝试使用Json.Net附带的JObject。它的行为很像Dictionary

以下是一个示例:

[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);
        }
    }
}

答案 2 :(得分:1)

自己找到解决方案:

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();

答案 3 :(得分:1)

使用.Net 4.5的 DataContractSerializer ,您可以为JSON元素指定任何变量名称,同时使用“Name”属性指定其实际名称。

所以你的课程看起来像:

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

     ...
}

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