Newtonsoft.Json使用

一、NuGet包提交Newtonsoft.Json

二、引用命名空间
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

三、命名空间
1、Newtonsoft.Json
JsonConvert,json字符串转换为object对象

string json = JsonConvert.SerializeObject(new { status = "y", info = "success", data = new string{ } });
//结果:{"status":"y","info":"success"}

 JsonSerializer
JsonReader 转 JObject

方式一

JObject jo = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader(@"{""status"" : ""y"", ""info"" : ""success""}")));

方式二 

using (JsonTextReader reader = new JsonTextReader(new StringReader(@"{""status"" : ""y"", ""info"" : ""success""}")))
{
    JObject jo = (JObject)JToken.ReadFrom(reader);
}

方式三 

using (System.IO.StreamReader file = System.IO.File.OpenText(@"D:data.json"))
{
    using (JsonTextReader reader = new JsonTextReader(file))
    {
        JObject jo = (JObject)JToken.ReadFrom(reader);
    }
}

 

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对象转为json字符串

string json = JsonConvert.SerializeObject(new { status = "y", info = "success", data= "success"});
//结果:{"status":"y","info":"success"}

 字符串转为JToken索引对象,取得属性,取得数组

JToken json = JValue.Parse(json);
string info = (string)json["info"];
JArray jarray = (JArray)json["data"];
foreach (dynamic arr in jarray)
int id = (int)arr["id"];

 JObject 用来生成一个JSON对象,简单来说就是生成”{}”,
JArray 用来生成一个JSON数组,也就是”[]”,
JProperty 用来生成一个JSON数据,格式为key/value的值,
JValue 直接生成一个JSON值

创建JSON字符串

JObject jo = new JObject();
jo.Add(new JProperty("status", "y"));
jo.Add(new JProperty("info", "success"));
string json = jo.ToString();
//结果:{ "status": "y", "info": "success" }

 创建JSON数组字符串

JArray arr = new JArray();
arr.Add(new JValue(1));
arr.Add(new JValue(2));
arr.Add(new JValue(3));
string json = arr.ToString();
//结果:[ 1, 2, 3 ]

 创建JSON数组对象字符串

JArray arr = new JArray();
	arr.Add(new JObject(){
	new JProperty("status", "y"),
	new JProperty("info", "success")
});
arr.Add(new JObject(){
	new JProperty("status", "n"),
	new JProperty("info", "failed")
});
string json = arr.ToString();
//结果:[ { "status": "y", "info": "success" }, { "status": "n", "info": "failed" } ]

 创建JSON字符串 与 JSON数组对象字符串

JObject jo = new JObject();
jo.Add(new JProperty("status", "y"));
jo.Add(new JProperty("info", "success"));
jo.Add(new JProperty("data", new JArray() {
	new JObject(){
		new JProperty("id", "1"),
		new JProperty("title", "hello")
	},
	new JObject(){
		new JProperty("id", "1"),
		new JProperty("title", "world")
	}
}));
//结果:{ "status": "y", "info": "success", "data": [ { "id": "1", "title": "hello" }, { "id": "1", "title": "world" } ] }

 将json转换为JObject

string json = "{ "status": "y", "info": "success" }";
JObject jo = JObject.Parse(json);
//判断是否存在属性名,
if(jo.ContainsKey("info"))
{
	//取值
	string value1 = jo["info"].ToString();			//结果:success
	string value2 = jo.GetValue("info").ToString(); //结果:success
}

 将Object转换为JObject

object obj = new { status = "y", info = "success"};
JObject jo = JObject.FromObject(obj);
string value = jo["info"].ToString();			//结果:success
	
//将json转换为JObject,读JSON数组数据
string json = "{ "status": "y", "info": "success", "data": [ { "id": "1", "title": "hello" }, { "id": "1", "title": "world" } ] }";
JObject jo = JObject.Parse(json);
IEnumerable<string> arr = from p in jo["data"].Children() select (string)p["title"];
foreach (var title in arr)
{
	templateBuilder.AppendLine(title);
}

 删除属性

JObject jo = JObject.Parse("JSON字符串");
jo.Remove("属性名");

 在属性后面添加属性

JObject jo = JObject.Parse("JSON字符串");
jo["属性名"].Parent.AddAfterSelf("属性");
原文地址:https://www.cnblogs.com/sntetwt/p/9510532.html