使用Newtonsoft.json转换json数据 [英] convert json data using Newtonsoft.json
本文介绍了使用Newtonsoft.json转换json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友们,我正在尝试将json数据转换为我在通用列表中的类但我收到此错误
无法转换类型'System.Collections.Generic.Dictionary< string,对象>'到'字符串'
请指导我,我很困惑我缺少的地方或如何获得这些数据。
字符串result1 =aab234cf34d
{
数据:
[
{
id:xxxxxxxxxxxxxxxxxxx,
first_name:aaaaaa,
last_name:bbbbb,
email:[eeeeeee,ffffff]
},
{
id:yyyyyyyyyyyyyyyyyy,
first_name:cccccccccc,
last_name:ddddddd,
电子邮件:[hhhhhhhh,ggggggg]
}
]
};
class MyClass
{
public string id {get;组; }
public string first_name {get;组; }
public string last_name {get;组; }
public string [] email {get;组; }
}
var jss = new System.Web.Script.Serialization.JavaScriptSerializer();
dynamic dataJss = jss.Deserialize< dynamic>(result1);
列表< MyClass> myList = new List< MyClass>();
foreach(dataJss [data]中的字符串键)
{
MyClass l = Newtonsoft.Json.JsonConvert.DeserializeObject< MyClass>(key);
myList.Add(l);
}
解决方案
使用Newtonsoft.Json或Microsoft的JavaScriptSerializer,而不是两者。
您现在使用jss.Deserialize< dynamic>(result1)
行创建一个IDictionary< string,object> ;
,其中每个对象
再次是IDictionary< string,object>
。
要使用Newtonsoft.Json,请执行以下操作:
JObject json = JObject.Parse(result1) ;
foreach (JObject o in json [ data])
{
MyClass mc = new MyClass
{
id = o [ id]。值< string> (),
first_name = o [ first_name]。值< string>() ,
last_name = o [ last_name]。值< string>(),
email = o [ email]。值< string>()。ToArray() // ToArray来自Linq,值< T>()将返回IEnumerable< T>和ToArra y()将从中生成一个数组。
};
// 等......
}
Hi friends I am trying to convert json data to my Class in a generic list but I am getting this error
Cannot convert type 'System.Collections.Generic.Dictionary<string,object>' to 'string'
Please guide me I am confused where I am lacking or how can i get this data.
string result1 = "aab234cf34d { "data": [ { "id": "xxxxxxxxxxxxxxxxxxx", "first_name": "aaaaaa", "last_name": "bbbbb", "email": [ "eeeeeee","ffffff" ] }, { "id": "yyyyyyyyyyyyyyyyyy", "first_name": "cccccccccc", "last_name": "ddddddd", "email": [ "hhhhhhhh","ggggggg" ] } ] }"; class MyClass { public string id { get; set; } public string first_name { get; set; } public string last_name { get; set; } public string[] email { get; set; } } var jss = new System.Web.Script.Serialization.JavaScriptSerializer(); dynamic dataJss = jss.Deserialize<dynamic>(result1); List<MyClass> myList = new List<MyClass>(); foreach (string key in dataJss["data"]) { MyClass l =Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(key); myList.Add(l); }
解决方案
Either use Newtonsoft.Json or Microsoft's JavaScriptSerializer, not both.
What you do now with the linejss.Deserialize<dynamic>(result1)
you create anIDictionary<string,object>
, where eachobject
is again anIDictionary<string,object>
.
To use Newtonsoft.Json, do something like this:
JObject json = JObject.Parse(result1); foreach (JObject o in json["data"]) { MyClass mc = new MyClass { id = o["id"].Value<string>(), first_name = o["first_name "].Value<string>(), last_name = o["last_name "].Value<string>(), email = o["email"].Values<string>().ToArray() // ToArray is from Linq, Values<T>() will return an IEnumerable<T>, and ToArray() will make an array out of it. }; // etc... }
这篇关于使用Newtonsoft.json转换json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文