获取Json Data C#

时间:2019-03-04 09:52:23

标签: c# json

我有一个函数,可以从中获取所有JSON数据,但是我不知道如何挑选特定数据。

我正在使用动态变量循环循环所有JSON信息。

例如,如何从给定的JSON URL中选择所有“名称:”并将其显示在控制台中。

使用网址获取数据:

    public string FetchData(string url)
    {
        string String_jsonData;

        using (WebClient client = new WebClient())
        {
            Uri SourceUri = new Uri(url);

            String_jsonData = client.DownloadString(SourceUri);
        }
        return String_jsonData;
    }

主要运行方法,

    static void Main(string[] args)
    {
        Program p = new Program();
        p.run();
        Console.ReadKey();

    }

    public void run()
    {
        string url = "JSON URL;

        dynamic pers = JsonConvert.DeserializeObject(FetchData(url));

        // fetching all data. Here I want to select specific data and save it.
        foreach (var item in pers)
        {
            Console.WriteLine(item);
        }

    }

模型类:

class Person
{
    public string name { get; set; }

    public string ci_list { get; set; }
}

这是我要提取的数据的示例

{
"items": [
{
"ci_type": "xxx",
"exists_in_equipman": false,
"exists_in_hydra": false,
"exists_in_taxen": true,
"hns_name": false,
"id": xxx,
"latest_sync_info": {
  "HYDRA": "xxx",
  "TAXEN": "xxx"
},
"modified_url": "xxx",
"name": "xxx",
"params": {
  "Band (4G)": {
    "id": xxx,
    "source": "script",
    "source_name": "xxx",
    "value": {}
  },
  "Hardware Specification": {
    "id": xxx,
    "source": "script",
    "source_name": "taxen_import",
    "value": {
      "0": {
        "3GPP release": {
          "value": ""
        },
        "Accessories": {
          "value": ""
        },
        "Category": {
          "value": ""
        },
        "Hw rev": {
          "value": "xxx"
        },
        "Locked": {
          "value": ""
        },
        "Model family name": {
          "value": "xxxx"
        },
        "Physical state": {
          "value": "xxxx"
        },
        "Serial No": {
          "value": "xxxx"
        },
        "Software": {
          "value": ""
        },
        "Software version": {
          "value": ""
        }
      }
    }
  },

2 个答案:

答案 0 :(得分:1)

首先从url

获取数据
var json = string.Empty;

string url = "http://";

using (var httpClient = new HttpClient())
{
    json = httpClient.GetStringAsync(url).Result;

}

然后反序列化对象(假设是避免对象)

 List<dynamic> list = JsonConvert.DeserializeObject<List<dynamic>>(json);

foreach (var ar in list)
{
   Console.WriteLine(ar["name"]); //assume object look like {"name":"saa", "age":"23"} and want to access name 
}

如何根据问题中的json对象提取ci_type值的示例

string json = "{\r\n\"items\": [\r\n{\r\n\"ci_type\": \"xxx\",\r\n\"exists_in_equipman\": false,\r\n\"exists_in_hydra\": false\r\n}\r\n]\r\n}";
var result = JsonConvert.DeserializeObject<dynamic>(json);
var data  = result["items"];

foreach (var cityType in data)
{
    Console.WriteLine(cityType["ci_type"]);
}

结果:xxx

答案 1 :(得分:0)

您的问题是pers不是直接数组,而是一个对象。

您需要遍历其中包含的“ items”属性。该数组包含具有“名称”属性的对象。

将循环代码更改为

foreach (var item in pers.items)