JSON上的c#JavaScriptSerializer包含字符串+;词典
我可以知道如何解析如下所述的JSON吗。。。。。JSON是Yahoo OAuth联系人列表的一部分 JSON: C#对象:JSON上的c#JavaScriptSerializer包含字符串+;词典,c#,javascriptserializer,C#,Javascriptserializer,我可以知道如何解析如下所述的JSON吗。。。。。JSON是Yahoo OAuth联系人列表的一部分 JSON: C#对象: 私有类字段 { 公共字符串id{get;set;} 公共字符串类型{get;set;} //公共字符串值{get;set;}//卡在这里!!!! //公共字典值{get;set;}//卡在这里!!!! } 如何解析“值”??因为它是字符串和字典的组合类型。我不能使用JavaScriptSerializer来回答它,但您可以使用和Linq来回答它 检查类似的问题我对解析
私有类字段
{
公共字符串id{get;set;}
公共字符串类型{get;set;}
//公共字符串值{get;set;}//卡在这里!!!!
//公共字典值{get;set;}//卡在这里!!!!
}
如何解析“值”??因为它是字符串和字典的组合类型。我不能使用JavaScriptSerializer来回答它,但您可以使用和Linq来回答它
检查类似的问题我对解析json知之甚少,但我可以告诉你,在同一个类中不能有两个字段具有相同的名称,因此,在fields类中同时使用string和Dictionary命名的value将引发编译器错误。您可能希望查看支持
动态
类型的JSON反序列化程序。@wgraham,是的。。。我现在正在找哦。。谢谢我现在正在试。是jObj=Newtonsoft.Json.Linq
???它不包含Select
@user2402624的定义,您只需要使用Newtonsoft.Json代码>和使用Newtonsoft.Json.Linq代码>太好了!!我对你的代码做了一些修改,这对我来说很有用。。。非常感谢。
"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"
},
}],
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 !!!!
}
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; }
}
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""
}
}
]}";