0

这是我拥有的 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"}
}

更好地查看数据:

更好的视野

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

4

4 回答 4

2

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

除此之外,您的问题太笼统了。有很多方法可以去做。简单的方法是创建一个类,调用它Product的定义类似于;

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

01在序列化之前,将and重写02oneand twoInnerData应该看起来像;

 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);
于 2013-05-01T20:03:53.163 回答
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);
        }
    }
}
于 2013-05-01T20:08:56.797 回答
1

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

所以你的班级可能看起来像:

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

     ...
}

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

于 2013-05-01T22:51:46.670 回答
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();
于 2013-05-01T20:22:25.523 回答