Json.Net从JObject循环更改为JArray

时间:2015-01-21 19:05:33

标签: c# json json.net

我正在提取一个看起来像这样的JSON Feed。

{
    "bank": [
        {
            "id": 35,
            "name": "bank 1",
            "endpoints": [
                {
                    "epId": 407,
                    "epName": "FRED001"
                },
                {
                    "epId": 516,
                    "epName": "FRED002"
                },
                {
                    "epId": 625,
                    "epName": "FRED003"
                }
            ]
        },
        {
            "id": 32,
            "name": "bank 2",
            "endpoints": [
                {
                    "epId": 426,
                    "epName": "HENRY001"
                },
                {
                    "epId": 553,
                    "epName": "HENRY002"
                }
            ]
        },
        {
            "id": 20,
            "name": "bank 3",
            "endpoints": [
                {
                    "epId": 1802,
                    "epName": "GEORGE001"
                },
                {
                    "epId": 920,
                    "epName": "GEORGE002"
                },
                {
                    "epId": 1052,
                    "epName": "GEORGE003"
                }
            ]
        }
    ]
}

最终目标是使用已知的epName'FRED001'进行搜索,并检索相应的epId'407,输出到MessageBox。

我可以通过使用循环在C#中执行此操作,例如:

JObject jResults = JObject.Parse(jsonfeed);
JToken jResults_bank = jResults["bank"];

foreach (JObject bank in Results_bank)
{
    JToken jResults_bank_endpoint = bank["endpoints"];
    foreach (JObject endpoint in jResults_bank_endpoint)
    {
        if (bank["epName"].ToString() == "FRED001")
                {
            MessageBow.Show(bank["epId"].ToString());
        }
    }
}

然而,这似乎不是最好的方法,我相信我应该通过构建一个数组在C#中做到这一点。如何使用JArray获得相同的结果?

3 个答案:

答案 0 :(得分:2)

无需显式循环更改您的解析方式 - 您只需使用带有JSON的LINQ:

using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;

class Test
{
    static void Main(string[] args)
    {
        string text = File.ReadAllText("Test.json");
        JObject json = JObject.Parse(text);
        var ids = from bank in json["bank"]
                  from endpoint in bank["endpoints"]
                  where (string) endpoint["epName"] == "FRED001"
                  select (string) endpoint["epId"];
        foreach (var id in ids)
        {
            Console.WriteLine(id);
        }
    }
}

答案 1 :(得分:0)

或者简单的方法就是像这样循环遍历它。

        JObject json = JObject.Parse(text);

        foreach (var id in json["bank"])
        {
           // your condition here
            if(id["epName"]=="FRED001")
            Console.WriteLine(id);
        }

答案 2 :(得分:0)

                JObject JObj = (JObject)yourjsontext;
                JArray JArr = (JArray)JObj["bank"];
                for (int i = 0; i < JArr.Count; i++)
                {
                    Console.WriteLine(JArr[i]["id"].ToString());
                    Console.WriteLine(JArr[i]["name"].ToString());

                    foreach (var endpoints in JArr[i]["endpoints"])
                    {
                        Console.WriteLine(endpoints["epId"].ToString());
                        Console.WriteLine(endpoints["epName"].ToString());
                    }
                }