一、NuGet包提交Newtonsoft.Json
二、引用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
三、命名空间
1、Newtonsoft.Json
JsonConvert,json字符串转换为object对象
object obj = JsonConvert.DeserializeObject(objstring);
JsonConverter
JsonSerializer
JsonReader
JsonReader reader = new JsonTextReader(new StringReader(@"{""input"" : ""value"", ""output"" : ""result""}"));
JsonWriter
2、Newtonsoft.Json.Linq
JObject
JObject jObject = JObject.Parse(objstring);
JObject obj = new JObject();
obj["data"] = 1;
string jsonstring = obj.ToString();
JArray
JArray jArray = JArray.Parse(jObject["data"].ToString());
foreach (var v in jArray){}
var value=jArray[a].ToString()
JArray array = new JArray();
array.Add("1");
JValue
JProperty
3、Newtonsoft.Json.Serialization
反序列化
object对象转为字符串
string datastring = JsonConvert.SerializeObject(result.data);
字符串转为JToken索引对象
JToken json = JValue.Parse(datastring);
取得属性
int id = (int)json["id"];
取得数组
JArray jarray = (JArray)json["jarray"];
foreach (dynamic arr in jarray)
int id = (int)arr["id"];