解析使用JSON数组Json.Net [英] Parsing a JSON array using Json.Net

查看:181
本文介绍了解析使用JSON数组Json.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Json.Net工作来解析数组。我正在试图做的是拉名称/值对出数组,并将其分配给特定变量在解析JObject。

I'm working with Json.Net to parse an array. What I'm trying to do is to pull the name/value pairs out of the array and assign them to specific variables while parsing the JObject.

下面是我在数组中已经得到了:

Here's what I've got in the array:

[
  {
    "General": "At this time we do not have any frequent support requests."
  },
  {
    "Support": "For support inquires, please see our support page."
  }
]

和这里就是我在C#中已经得到了:

And here's what I've got in the C#:

WebRequest objRequest = HttpWebRequest.Create(dest);
WebResponse objResponse = objRequest.GetResponse();
using (StreamReader reader = new StreamReader(objResponse.GetResponseStream()))
{
    string json = reader.ReadToEnd();
    JArray a = JArray.Parse(json);

    //Here's where I'm stumped

}

我是相当新的JSON和Json.Net,所以它可能是别人一个基本的解决方案。我基本上只需要指定名称/值对在foreach循环,这样我可以输出的前端数据。有没有人这样做呢?

I'm fairly new to JSON and Json.Net, so it might be a basic solution for someone else. I basically just need to assign the name/value pairs in a foreach loop so that I can output the data on the front-end. Has anyone done this before?

推荐答案

您可以在这样的数据值得到:

You can get at the data values like this:

string json = @"
[ 
    { ""General"" : ""At this time we do not have any frequent support requests."" },
    { ""Support"" : ""For support inquires, please see our support page."" }
]";

JArray a = JArray.Parse(json);

foreach (JObject o in a.Children<JObject>())
{
    foreach (JProperty p in o.Properties())
    {
        string name = p.Name;
        string value = (string)p.Value;
        Console.WriteLine(name + " -- " + value);
    }
}

小提琴: https://dotnetfiddle.net/uox4Vt

这篇关于解析使用JSON数组Json.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆