簡體   English   中英

C# 迭代 JSON object

[英]C# Iterate over a JSON object

我正在嘗試使用.Net Json 反序列化器System.Text.Json來迭代以下 Json 輸入。

如果“person_id”是 json 結構的頂層(見下文),我已經找到了通過“person_id”進行迭代的方法,但我無法找到循環遍歷所有下一級項目的方法。

我認為 NewtonSoft JArrayJObject非常接近我需要的東西(我可能會追求),但不確定微軟的解決方案是否有辦法做到這一點......

微軟的System.Text.Json庫可以做到這一點嗎?

{
  "0": {
    "person_id": "0",
    "last_name": "Greg",
    "first_name": "Craig",
  },
  "1": {
    "person_id": "1",
    "last_name": "Doe",
    "first_name": "John",
  }
}

用於通過屬性名稱提取 object 的JsonElement解決方案(即我可以通過這種方式獲取 John Doe 的信息)。

using (JsonDocument document = JsonDocument.Parse(jsonString))
{
    JsonElement root = document.RootElement;

    JsonElement studentsElement = root.GetProperty("1");
}

看起來類似於我之前必須解決的一些 JSON。 它有助於記住 JSON 對象只是鍵/值對。

考慮到這一點,您擁有的數據結構可以解釋為 C# 中的Dictionary

我在我的示例中使用了Newtonsoft.Json ,但它們很容易轉換為使用System.Text.Json

以最簡單的形式,您可以使用:

var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);

這會給你一個Dictionary ,其中鍵是"0""1"屬性,值是 object 代表里面的人員信息。

然后,您可以在 Dictionary 或其鍵或值上運行一個簡單的foreach循環,如下所示:

foreach (var item in dictionary)
{
    var key = item.Key;
    var person = item.Value;
}

但是這個personitem.Value )仍然只有object類型。

在這里您可以創建一個 class 來表示每個Person的信息,裝飾屬性以匹配 JSON 數據結構中的內容,如下所示:

public class Person 
{
    [JsonProperty("person_id")]
    public string PersonId { get; set; }
    
    [JsonProperty("first_name")]
    public string FirstName { get; set; }
    
    [JsonProperty("last_name")]
    public string LastName { get; set; }
}

而是將您的 JSON 反序列化為Dictionary<string, Person>

var dictionary = JsonConvert.DeserializeObject<Dictionary<string, Person>>(jsonString);

這意味着字典中條目的每個值都是Person的一個實例,您可以訪問每個條目的強類型屬性,如下所示:

foreach (var item in dictionary)
{
    var key = item.Key; // although you probably won't need this
    Console.WriteLine(key);

    var person = item.Value;
    Console.WriteLine(person.PersonId);
    Console.WriteLine(person.FirstName);
    Console.WriteLine(person.LastName);
}

// which gives the following output:
> 0
> 0
> Craig
> Greg
> 1
> 1
> John
> Doe


雖然我在上面使用過Newtonsoft.Json ,但很容易“遷移”上面的示例以使用System.Text.Json

  • 代替JsonConvert.DeserializeObject<T> ,使用JsonSerializer.Deserialize<T>
  • 除了使用[JsonProperty("property_name")]屬性,您可以使用[JsonPropertyName("property_name")]

希望這可以幫助。

您可以使用JsonElement.EnumerateObject 用於枚舉此JsonElement表示的JSON object中的屬性

var options = new JsonDocumentOptions 
{
   AllowTrailingCommas = true,
   CommentHandling = JsonCommentHandling.Skip
};
using (JsonDocument document = JsonDocument.Parse(jsos, options))
{
    foreach (JsonProperty property in document.RootElement.EnumerateObject())
    {
        string key = property.Name;
        JsonElement valueElement = property.Value;
        string person_id = valueElement.GetProperty("person_id").GetString();
        string last_name = valueElement.GetProperty("last_name").GetString();
        string first_name = valueElement.GetProperty("first_name").GetString();
     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM