如何循环遍历 C# 中的嵌套 JSON 对象?
How to loop through nested JSON objects in C#?
我正在开发一个 C# 项目,我需要调用 API 并解析 JSON 响应。
JSON 响应:
{
"version": "3.9.1",
"data": {
"obj1": {
"version": "1.0.0",
"id": "obj1",
"name": "Object 1",
"title": "Object 1",
"info": {
"info1": 8,
"info2": 4,
"info3": 3
},
"image": {
"full": "Object1.png",
"w": 64,
"h": 64
},
"tags": [
"Tag1",
"Tag2"
]
},
"obj2": {
"version": "1.0.0",
"id": "obj2",
"name": "Object 2",
"title": "Object 2",
"info": {
"info1": 1,
"info2": 6,
"info3": 7
},
"image": {
"full": "Object2.png",
"w": 64,
"h": 64
},
"tags": [
"Tag1",
"Tag6"
]
},
"obj3": {
"version": "1.0.0",
"id": "obj3",
"name": "Object 3",
"title": "Object 3",
"info": {
"info1": 0,
"info2": 1,
"info3": 2
},
"image": {
"full": "Object3.png",
"w": 64,
"h": 64
},
"tags": [
"Tag7",
"Tag8"
]
}
}
}
有了这个响应,我想遍历位于 data
内的所有对象。 data
中的对象数量并不总是相同;有可能只有一个对象,也有可能只有十个对象。
我 运行 遇到的问题是我无法遍历 data
属性,因为出现以下错误:
foreach statement cannot operate on variables of type 'Objects' because 'Objects' does not contain a public instance or extension definition for 'GetEnumerator'
我知道为什么会收到错误消息,但我找不到解决问题的方法。
我的代码:
MainWindow.xaml.cs
WebRequest request = WebRequest.Create(path);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ReaderJson = reader.ReadToEnd();
var JsonResponse = JsonConvert.DeserializeObject<Response>(ReaderJson);
foreach (var obj in JsonResponse.data)
{
Console.WriteLine(obj.ToString());
}
JsonParser.cs
public class Response
{
public string version { get; set; }
public Objects data { get; set; }
}
public class Objects
{
public string id { get; set; }
public string name { get; set; }
public string title { get; set; }
public Info info { get; set; }
public Images images { get; set; }
}
public class Info
{
public int info1 { get; set; }
public int info2 { get; set; }
public int info3 { get; set; }
}
public class Images
{
public string full { get; set; }
public int w { get; set; }
public int h { get; set; }
}
如何在不使用 obj1
、obj2
等调用的情况下遍历 data
中的所有对象?
这里的问题是您的 JSON 模式为 data
定义了一个 字典 ,但是您的 data
属性 returns 单个实例 Objects
class.
数据模型
你想要的是 Response
class 看起来像这样:
public class Response
{
public string version { get; set; }
public Dictionary<string, Objects> data { get; set; }
}
其他都应该没问题。
循环数据
鉴于上述情况,您现在可以使用类似以下内容的方式遍历 data
字典:
foreach (var obj in JsonResponse.data)
{
Console.WriteLine(obj.Key);
}
Note: When looping through a Dictionary<TKey, TValue>
, an enumeration of KeyValuePair<TKey, TValue>
s will be returned, with your key (i.e., obj1
) in the Key
property, and your object in the Value
property.
命名约定
显然,名称 Objects
在这里没有意义,因为它表示单个对象,而不是对象的集合。为了与您的代码保持一致,我保留它 Objects
,但您需要给它一个单数名称。我假设这是您希望它代表一个集合时遗留下来的。
我正在开发一个 C# 项目,我需要调用 API 并解析 JSON 响应。
JSON 响应:
{
"version": "3.9.1",
"data": {
"obj1": {
"version": "1.0.0",
"id": "obj1",
"name": "Object 1",
"title": "Object 1",
"info": {
"info1": 8,
"info2": 4,
"info3": 3
},
"image": {
"full": "Object1.png",
"w": 64,
"h": 64
},
"tags": [
"Tag1",
"Tag2"
]
},
"obj2": {
"version": "1.0.0",
"id": "obj2",
"name": "Object 2",
"title": "Object 2",
"info": {
"info1": 1,
"info2": 6,
"info3": 7
},
"image": {
"full": "Object2.png",
"w": 64,
"h": 64
},
"tags": [
"Tag1",
"Tag6"
]
},
"obj3": {
"version": "1.0.0",
"id": "obj3",
"name": "Object 3",
"title": "Object 3",
"info": {
"info1": 0,
"info2": 1,
"info3": 2
},
"image": {
"full": "Object3.png",
"w": 64,
"h": 64
},
"tags": [
"Tag7",
"Tag8"
]
}
}
}
有了这个响应,我想遍历位于 data
内的所有对象。 data
中的对象数量并不总是相同;有可能只有一个对象,也有可能只有十个对象。
我 运行 遇到的问题是我无法遍历 data
属性,因为出现以下错误:
foreach statement cannot operate on variables of type 'Objects' because 'Objects' does not contain a public instance or extension definition for 'GetEnumerator'
我知道为什么会收到错误消息,但我找不到解决问题的方法。
我的代码:
MainWindow.xaml.cs
WebRequest request = WebRequest.Create(path);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ReaderJson = reader.ReadToEnd();
var JsonResponse = JsonConvert.DeserializeObject<Response>(ReaderJson);
foreach (var obj in JsonResponse.data)
{
Console.WriteLine(obj.ToString());
}
JsonParser.cs
public class Response
{
public string version { get; set; }
public Objects data { get; set; }
}
public class Objects
{
public string id { get; set; }
public string name { get; set; }
public string title { get; set; }
public Info info { get; set; }
public Images images { get; set; }
}
public class Info
{
public int info1 { get; set; }
public int info2 { get; set; }
public int info3 { get; set; }
}
public class Images
{
public string full { get; set; }
public int w { get; set; }
public int h { get; set; }
}
如何在不使用 obj1
、obj2
等调用的情况下遍历 data
中的所有对象?
这里的问题是您的 JSON 模式为 data
定义了一个 字典 ,但是您的 data
属性 returns 单个实例 Objects
class.
数据模型
你想要的是 Response
class 看起来像这样:
public class Response
{
public string version { get; set; }
public Dictionary<string, Objects> data { get; set; }
}
其他都应该没问题。
循环数据
鉴于上述情况,您现在可以使用类似以下内容的方式遍历 data
字典:
foreach (var obj in JsonResponse.data)
{
Console.WriteLine(obj.Key);
}
Note: When looping through a
Dictionary<TKey, TValue>
, an enumeration ofKeyValuePair<TKey, TValue>
s will be returned, with your key (i.e.,obj1
) in theKey
property, and your object in theValue
property.
命名约定
显然,名称 Objects
在这里没有意义,因为它表示单个对象,而不是对象的集合。为了与您的代码保持一致,我保留它 Objects
,但您需要给它一个单数名称。我假设这是您希望它代表一个集合时遗留下来的。