使用Newtonsoft.json转换json数据

人气:162 发布:2022-09-22 标签: 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 line jss.Deserialize<dynamic>(result1) you create an IDictionary<string,object>, where each object is again an IDictionary<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...
}

527