我有一個關于System.Text.Json.deserialize. 我有這樣的Json值
{
"id" :"1",
"ShopName" :"Paint Shop",
"Books" : [1,2]
}
它可以很容易地轉換為簡單的物件
public class bookstore {
public int id {get;set}
public string ShopName {get;set}
public IEnumerable<int> Books {get;set}
}
但是我有一個函式可以將書籍陣列 int 轉換為這樣的物件串列
{
"id" :"1",
"ShopName" :"Paint Shop",
"Books" : [
{
"bookId":1,
"bookName":"Peter Pan",
"location":"A01"
},
{
"bookId":2,
"bookName":"Cooking Book",
"location":"A02"
}
]
}
現在,我想Books[1].location通過jsonnode.parse(string)轉換物件來修改 value 的值。我可以知道怎么做嗎?
謝謝
uj5u.com熱心網友回復:
不知道你為什么要使用JsonNode.Parse().
如果你只是想修改location's 的值,也許你可以使用JsonSerializerwithBookStore和Bookclass 來幫助你。
??類Book是.IEnumerableBookStore
.NET6 Console App
??的一個例子BookClass是一個2 級嵌套類
// See https://aka.ms/new-console-template for more information
using System.Text.Json;
// Initialize json string
string jsonStr = "{\"id\": 1, \"ShopName\" :\"Paint Shop\", \"Books\" : [\r\n {\r\n \"bookId\":1,\r\n \"bookName\":\"Peter Pan\",\r\n \"location\":\"A01\"\r\n },\r\n {\r\n \"bookId\":2,\r\n \"bookName\":\"Cooking Book\",\r\n \"location\":\"A02\"\r\n }\r\n ]\r\n }";
// Deserilize to object
Bookstore bookstore = JsonSerializer.Deserialize<Bookstore>(jsonStr);
// Write the location's value
Console.WriteLine(bookstore.Books.ToArray()[1].location);
// Modified the location's value
bookstore.Books.ToArray()[1].location = "_Modified";
// Write the modified location's value
Console.WriteLine(bookstore.Books.ToArray()[1].location);
// See result output
Console.ReadLine();
public class Bookstore
{
public int id { get; set; }
public string ShopName { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class Book
{
public int bookId { get; set; }
public string bookName { get; set; }
public string location { get; set; }
}
結果輸出影像

如果您堅持使用JsonNode.Parse(),則需要更多步驟。像這樣。
// See https://aka.ms/new-console-template for more information
using System.Text.Json;
using System.Text.Json.Nodes;
// Initialize json string
string jsonStr = "{\"id\": 1, \"ShopName\" :\"Paint Shop\", \"Books\" : [\r\n {\r\n \"bookId\":1,\r\n \"bookName\":\"Peter Pan\",\r\n \"location\":\"A01\"\r\n },\r\n {\r\n \"bookId\":2,\r\n \"bookName\":\"Cooking Book\",\r\n \"location\":\"A02\"\r\n }\r\n ]\r\n }";
// Parse to jsonNode
JsonNode jsonNode = JsonNode.Parse(jsonStr);
// Convert to JsonObject
JsonObject jsonObject = jsonNode.AsObject();
// Convert Books to Json Array
JsonArray jsonArray = jsonObject["Books"].AsArray();
// Convert index 1 in array to Json object
JsonObject book1Object = jsonArray[1].AsObject();
// Write the location value
Console.WriteLine(book1Object["location"]);
// Modify location value
book1Object["location"] = "_modified";
// Write the modified location value
Console.WriteLine(book1Object["location"]);
// See output
Console.ReadLine();
public class Bookstore
{
public int id { get; set; }
public string ShopName { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class Book
{
public int bookId { get; set; }
public string bookName { get; set; }
public string location { get; set; }
}
其他建議
使用
筆記:
你應該添加Newtonsoft.Json包。
uj5u.com熱心網友回復:
由于您使用的是 System.Text.Json,因此您可以使用此代碼更改值
var jsonParsed=JsonNode.Parse(json);
jsonParsed["Books"][1]["location"]="A03";
在此之后,如果您需要,您可以獲得更新的 json
json = System.Text.Json.JsonSerializer.Serialize(jsonParsed, new JsonSerializerOptions {WriteIndented=true});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517058.html
標籤:C#json.net-coresystem.text.json
下一篇:在間諜視窗中添加變數時,如何在excelvba中觀看超過256個元素的“Scripting.Dictionary”?
