我是否知道如何解析下面所述的JSON ...... JSON是Yahoo OAuth联系人列表的一部分。
JSON:
"fields":[{
"id":2,
"type":"nickname",
"value":"Hello"
},
{
"id":3,
"type":"email",
"value":"MyTesting@hotmail.com"
},
{
"id":1,
"type":"name",
"value":{
"givenName":"Otopass",
"middleName":"Test",
"familyName":"Hotmail"
},
}],
C#对象:
private class fields
{
public string id { get; set; }
public string type { get; set; }
//public string value { get; set; } //Stuck At Here !!!!
//public Dictionary<string, string> value { get; set; } //Stuck At Here !!!!
}
如何解析“价值”?因为它是String&amp;的组合类型。字典。
答案 0 :(得分:0)
我无法使用JavaScriptSerializer回答它,但你可以使用json.Net和Linq
来回答var jObj = JObject.Parse(json);
var fields = jObj["fields"]
.Select(x => new Field
{
Id = (int)x["id"],
Type = (string)x["type"],
Value = x["value"] is JValue
? new Dictionary<string,string>(){{"",(string)x["value"]}}
: x["value"].Children()
.Cast<JProperty>()
.ToDictionary(p => p.Name, p => (string)p.Value)
})
.ToList();
private class Field
{
public int Id { get; set; }
public string Type { get; set; }
public Dictionary<string, string> Value { get; set; }
}
PS:我将你的部分json字符串修复为
string json =
@"{""fields"":[
{
""id"":2,
""type"":""nickname"",
""value"":""Hello""
},
{
""id"":3,
""type"":""email"",
""value"":""MyTesting@hotmail.com""
},
{
""id"":1,
""type"":""name"",
""value"":{
""givenName"":""Otopass"",
""middleName"":""Test"",
""familyName"":""Hotmail""
}
}
]}";