簡體   English   中英

在 C# 中查詢 JSON

[英]Querying JSON in C#

我是使用 JSON 的新手,我想出了如何查詢特定數據,但它如何與 foreach 循環一起工作?

這是我的 JSON:

{
    "Example": {
        "Header": [
            [
                "A",
                "B",
                "C",
                "D",
                "E",
                "F",
                "G",
                "H",
                "I",
                "J",
                "K"
            ]
        ],
        "Body": [{
                "A": "XY",
                "B": 0,
                "C": 5,
                "D": 2,
                "E": 5,
                "F": 7,
                "G": 3,
                "H": 7,
                "I": 2,
                "J": 7,
                "K": 88
            },
            {
                "A": "XY",
                "B": 4,
                "C": 3,
                "D": 5,
                "E": 8,
                "F": 6,
                "G": 4,
                "H": 2,
                "I": 887,
                "J": 445,
                "K": 4
            },
            {
                                "A": "XY",
                "B": 0,
                "C": 20,
                "D": 32,
                "E": 44,
                "F": 5,
                "G": 50,
                "H": 2,
                "I": 35,
                "J": 10,
                "K": 55
            }
        ]
    }
}

我需要獲取每個區域的整數。

我發現我可以得到這樣的特定數據(我得到的值為 0)

public void importLocations(JObject locationsObject) 
{
    string test = (string) MyObject.SelectToken("Body[1].A");
}

我正在嘗試恢復所有值,但我無法做到。 我會很感激你的幫助。

編輯:這對我有用:

var bodys = MyObject["Body"] as JArray;
int counter = bodys.Count;

for (int x = 0; x < counter; x++) 
{
    foreach(var i in bodys[x]) 
    {
        i.First.Value < string > ();
        Console.WriteLine(i);
    }
}

如果你知道一個更優雅的選擇,我也很樂意知道。

如果您的 JSON 架構是固定的,則接受的答案會很有效。 它具有使代碼易於閱讀的優點。 如果架構可能會更改並且您希望避免更新比您需要的更多的屬性,您可以嘗試以下操作:

    var test = locationsObject.SelectTokens("LocationDistance.Body[*]")
            .Select(t => t.Children()
                .Select(c => c.ToObject<JProperty>())
                .Where(p => p.Name.StartsWith("Zone "))
                .Select(t => new { Name = t.Name, Value = int.Parse(t.Value.ToString()) }))
            .ToList();

將您的 JSON 反序列化為一個類,然后做任何您想做的事情。 網絡小提琴

public class Body
{
  public string Abstände { get; set; }
  public int Eingangslager { get; set; }

  [JsonProperty(PropertyName = "Zone A")]
  public int ZoneA { get; set; }

  [JsonProperty(PropertyName = "Zone B")]
  public int ZoneB { get; set; }

  [JsonProperty(PropertyName = "Zone C")]
  public int ZoneC { get; set; }

  [JsonProperty(PropertyName = "Zone D")]
  public int ZoneD { get; set; }

  [JsonProperty(PropertyName = "Zone E")]
  public int ZoneE { get; set; }

  [JsonProperty(PropertyName = "Zone F")]
  public int ZoneF { get; set; }

  [JsonProperty(PropertyName = "Zone G")]
  public int ZoneG { get; set; }

  [JsonProperty(PropertyName = "Zone H")]
  public int ZoneH { get; set; }

  public int Ausgangslager { get; set; }
}

public class LocationDistance
{
  public List<List<string>> Header { get; set; }
  public List<Body> Body { get; set; }
}

public class Root
{
  public LocationDistance LocationDistance { get; set; }
}

public class Program
{
    public static void Main()
    {

    var data = JsonConvert.DeserializeObject<Root>(myJsonResponse);

    data.LocationDistance.Body.ForEach(body => {
      var propertyInfo = body.GetType().GetProperties();
      foreach (var p in propertyInfo)
      {
        Console.WriteLine(p.Name + " : " + p.GetValue(body));
      }
    });
  }

暫無
暫無

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

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