我正在使用Json.Net解析array.我try 做的是将名称/值对从数组中取出,并在解析JObject时将它们赋给特定的变量.

下面是我在数组中得到的信息:

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

下面是我在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循环中分配名称/值对,以便在前端输出数据.以前有人这样做过吗?

推荐答案

您可以获得如下数据值:

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

Asp.net相关问答推荐

在Docker Windows中,ASP.NET核心容器在自定义端口8080上运行,但ASP.NET容器在固定端口80上运行

从 asp.net 中的 Web.config 获取连接字符串

MSCharts找不到请求类型'GET'的http处理程序错误

ASP.NET MVC4 jquery/javascript 包的使用

如何从 web.config 中读取系统值并在 ASP.NET MVC C# 方法中使用

如何在不使用 Session 的情况下在 ASP.net 中的页面之间传递值

学习什么 - Ruby on Rails 或 ASP .NET MVC...鉴于熟悉 ASP .NET

我应该如何在类和应用层之间传递数据?

在 asp.net 中调整图像大小而不会丢失图像质量

如何从 SQL Server 2008 本身获取客户端 IP 地址?

如何从 ASP.Net 发布然后重定向到外部 URL?

如何使用 WebRequest 发布数据并从网页获取响应

如何保护存储在 web.config 中的密码?

如何获取程序集的最后修改日期?

HtmlAgilityPack Select 的子 node 不符合预期

当用户在文本框中按 Enter 键时执行按钮单击事件

人们使用 JScript.Net 的目的是什么?

ASP.NET 核心,更改未经授权的默认重定向

在控制器 asp.net-core 中获取当前区域性

DataTable 不包含 AsEnumerable 的定义