6.0 後多了 System.Text.Json.Nodes
基本上離完善接近 Newtonsoft.Json 的功能又跨一大步
之前大概能勝任序列化.反序列化.PARSE和READ
在6.0後多了 JsonObject , JsonArray
不需要簡單為了串一個JSON特別再定義一個CLASS去做反序列
Newtonsoft版本
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
private void button1_Click(object sender, EventArgs e)
{
List<string> list_org = new List<string>() { "aa", "bb", "cc" };
string list2jsonStr = JsonConvert.SerializeObject(list_org);
Console.WriteLine(list2jsonStr);
List<string> listfromjsonStr = new List<string>();
listfromjsonStr =
JsonConvert.DeserializeObject<List<string>>(list2jsonStr);
Console.WriteLine(listfromjsonStr[0]);
JObject jobj = new JObject();
JObject jobj2 = new JObject();
jobj2["aa"] = "11";
jobj2["Bu"] = "22";
jobj["aaa"] = "cc";
jobj["yy"] = jobj2;
Console.WriteLine(JsonConvert.SerializeObject(jobj));
}
System.Text.Json版本
https://tinyurl.com/34utw32c
https://tinyurl.com/4h3cnt4x
using System.Text.Json;
using System.Text.Json.Nodes;
private void button1_Click(object sender, EventArgs e)
{
List<string> list_org = new List<string>() { "aa", "bb", "cc" };
string list2jsonStr = JsonSerializer.Serialize(list_org);
Console.WriteLine(list2jsonStr);
List<string> listfromjsonStr = new List<string>();
listfromjsonStr =
JsonSerializer.Deserialize<List<string>>(list2jsonStr);
Console.WriteLine(listfromjsonStr[0]);
//.NET 6 後 從 System.Text.Json.Nodes 新增 JsonObject , JsonArray
JsonObject jobj = new JsonObject();
JsonObject jobj2 = new JsonObject();
jobj2["aa"] = "11";
jobj2["Bu"] = "22";
jobj["aaa"] = "cc";
jobj["yy"] = jobj2;
Console.WriteLine(JsonSerializer.Serialize(jobj));
}
現在多數的常見需要用到的功能 內建的都足以勝任了