JSON转Jtoken/Model/JObject
static void Main(string[] args) { try { string json = "{\"Name\":\"zhu\",\"Age\":\"15\",\"A\":[{\"Aa\":\"aa\"},{\"Bb\":\"bb\"}]}"; //将JOSN转成Object //DeserializeObject<T> 你要转成什么样的目标类型,JOject|Jtoken|Model... var objectStr = JsonConvert.DeserializeObject<JObject>(json); //这个是JObject? var obj = JObject.Parse(json);//这个是JObject var NameValue = obj.Value<string>("Name"); var NameSele = obj.SelectToken("Name", false); //将JSON转成对象 var model = JToken.Parse(json).ToObject<Rootx>(); // 将JSON转成对象 var model2 = JsonConvert.DeserializeObject<Rootx>(json); //将JSON转成Jtoken JToken joken = JToken.Parse(json); //将JSON转成Jtoken var jToken2 = JsonConvert.DeserializeObject<JToken>(json); //获取JSON中指定的key的值 var Age = jToken2.SelectToken("Age", false);//key有对应的Value var ww = jToken2.SelectToken("ww", false);//key值没有对应的Value ,就是false var JtokenA = jToken2.Value<JToken>("A"); //hasValues JToken才有的属性 !!!!!! var s = JtokenA.HasValues; //SelectToken 侧重于取值 //Value<T>("key") 侧重于把取出来的key值 再转换成<T>目标类型 //获取List 方式1:SelectToken 方式2:Value<T>() var list = jToken2.SelectToken("Name", false).SelectToken("order_info_list", false); var lists = jToken2.Value<JToken>("order_search").Value<JToken>("order_info_list"); //string json3 = "{\"ADDRESS_MAP\":{\"ADDRESS_LOCATION\":{\"type\":\"separator\",\"name\":\"Address\",\"value\":\"\",\"FieldID\":40},\"LOCATION\":{\"type\":\"locations\",\"name\":\"Location\",\"keyword\":{\"1\":\"LOCATION1\"},\"value\":{\"1\":\"United States\"},\"FieldID\":41},\"FLOOR_NUMBER\":{\"type\":\"number\",\"name\":\"Floor Number\",\"value\":\"0\",\"FieldID\":55},\"self\":{\"id\":\"2\",\"name\":\"Address Map\"}}}"; string json3 = "{\"ADDRESS_MAP\": { \"ADDRESS_LOCATION\": null, \"LOCATION\": { \"type\": \"locations\", \"name\": \"Location\", \"keyword\": { \"1\": \"LOCATION1\" }, \"value\": { \"1\": \"United States\" }, \"FieldID\": 41 }, \"FLOOR_NUMBER\": { \"type\": \"number\", \"name\": \"Floor Number\", \"value\": \"0\", \"FieldID\": 55 }, \"self\": { \"id\": \"2\", \"name\": \"Address Map\" } } }"; JToken jToken3 = JToken.Parse(json3); JObject AddressMap = jToken3["ADDRESS_MAP"].Value<JObject>(); //这里【ADDRESS_LOCATION】的value即使是null 值,也没报错 JObject AddressMap_Location = jToken3["ADDRESS_MAP"]["ADDRESS_LOCATION"].Value<JObject>(); //这个Properties()属性集合,就是key值的集合,P.Name 就是具体的Key List<string> keys = AddressMap.Properties().Select(p => p.Name).ToList(); foreach (string k in keys) { Console.WriteLine(k); } string str3 = "{\"Result\":{\"name\":\"zhu\"},\"Message\":\"折扣行的产品单位必需是空。\",\"ErrList\":[{\"XTLSH\":\"144115188078156755\",\"ErrMsg\":\"折扣行的产品单位必需是空。 \"}],\"SucessList\":[]}"; //转成JToken(JObject 基类) var objRet = JsonConvert.DeserializeObject<JToken>(str3); // 获取JSON里的xx键的值 string v1 = objRet.Value<JToken>("Result").Value<string>("name"); var v2 = objRet.SelectToken("Result", false).ToString(); Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(str3); string Result = jObject["Result"].ToString(); Console.ReadLine(); } catch (Exception ex) { throw ex; } string x = ""; }
人各有命,上天注定,有人天生为王,有人落草为寇。脚下的路,如果不是你自己的选择,那么旅程的终点在哪,也没人知道。你会走到哪,会遇到谁,都不一定。