我有一个从Web应用程序获得的JSON。我尝试使用System.Web.Script.Serialization解析对象。但是我的解析有问题
const String jsonWeb = @"{
""error"" : 0,
""result"" : {
""data"" : {
""1722278"" : {
""din"" : ""741338;361490;648450;934096;180467;209928;805500;283840;901917;110193;9438;363965;700670;647965;18399;31993;714755;242839;60436;600310;"",
""pid"" : ""11""
},
""1752279"" : {
""din"" : ""970510;771034;372305;286336;926373;459655;1409;140138;692017;311733;686476;368178;735954;635501;816346;821514;768444;510380;959990;916515;"",
""pid"" : ""11""
},
""1752280"" : {
""din"" : ""992662;474351;139725;998728;475954;706331;798728;744204;999194;22546;177003;4098;448658;842154;431042;970297;989184;362126;7720;720781;"",
""pid"" : ""11""
}
},
""pid"" : {
""11"" : {
""codejs"" : "" very \""long\"" String "",
""max"" : ""3""
}
},
""sys"" : {
""11"" : {
""1"" : {
""b"" : ""Firefox"",
""s"" : ""Windows""
},
""2"" : {
""b"" : ""Chrome"",
""s"" : ""Windows""
}
}
},
""maxgetlimit"" : 10001,
""setProxy"" : [],
""cidget"" : {
""111102"" : 1
},
""openBrowser"" : 1
}}";
我尝试使用此代码解析此但我收到错误。我试着用这个:
public class Pack
{
public string din { get; set; }
public string pid { get; set; }
}
public class MySybObject
{
public Pack attributes { get; set; }
}
public class Result
{
public Result() { data = new List<MySybObject>(); }
public List<MySybObject> data { get; set; }
}
public class Code
{
public String codejs { get; set; }
public String max { get; set; }
}
public class MySubPid
{
public Code code { get; set; }
}
public class SystemReq
{
public String b { get; set; }
public String s { get; set; }
}
public class Sys
{
public SystemReq sysreq { get; set; }
}
public class Cidget
{
public String cidget { get; set; }
}
public class Response
{
public Response() {}
public string error { get; set; }
public Result results { get; set; }
public MySubPid pid { get; set; }
public Sys sys { get; set; }
public Int32 maxgetlimit { get; set; }
public String setProxy { get; set; }
public Cidget cidget { get; set; }
public String openBrowser { get; set; }
}
JavaScriptSerializer ser = new JavaScriptSerializer();
Response foo = ser.Deserialize<Response>(jsonWeb);
但是我得到了错误,我可能在数据中丢失了一些对象 - 这对我来说是重要的数据。
答案 0 :(得分:2)
由于json字符串中有数字用作keys
,因此在这种情况下使用动态对象而不是反序列化为类更好(我为此使用了Json.Net)
dynamic jsonObj = JsonConvert.DeserializeObject(jsonWeb);
Console.WriteLine(jsonObj.result.data["1722278"].pid);
Console.WriteLine(jsonObj.result.sys["11"]["1"].b);
BTW:这条线错了
""codejs"" : "" very ""long"" String ""
它应该是这样的
""codejs"" : "" very \""long\"" String "",
<强> - 编辑 - 强>
foreach (var child in jsonObj.result.data.Children())
{
Console.WriteLine(child.Name + "=> " + child.Value.din);
}
答案 1 :(得分:0)
尝试使用JSON class generator来生成课程,然后再试一次。
更新: 我必须更改JSON字符串才能生成它;
{
"error" : 0,
"result" : {
"data" : {
"1722278" : {
"din" : "741338;361490;648450;934096;180467;209928;805500;283840;901917;110193;9438;363965;700670;647965;18399;31993;714755;242839;60436;600310;",
"pid" : "11"
},
"1752279" : {
"din" : "970510;771034;372305;286336;926373;459655;1409;140138;692017;311733;686476;368178;735954;635501;816346;821514;768444;510380;959990;916515;",
"pid" : "11"
},
"1752280" : {
"din" : "992662;474351;139725;998728;475954;706331;798728;744204;999194;22546;177003;4098;448658;842154;431042;970297;989184;362126;7720;720781;",
"pid" : "11"
}
},
"pid" : {
"11" : {
"codejs" : "very long String ",
"max" : "3"
}
},
"sys" : {
"11" : {
"1" : {
"b" : "Firefox",
"s" : "Windows"
},
"2" : {
"b" : "Chrome",
"s" : "Windows"
}
}
},
"maxgetlimit" : 10001,
"setProxy" : [],
"cidget" : {
"111102" : 1
},
"openBrowser" : 1
}}