【发布时间】:2016-12-30 02:20:42
【问题描述】:
我正在使用来自 API 的 json,这就是我所说的:
{
"popularity": 3.518962,
"production_companies": [
{
"name": "value1",
"id": 4
},
{
"name": "value2",
"id": 562
},
{
"name": "value13",
"id": 14654
},
{
"name": "value4",
"id": 19177
},
{
"name": "value5",
"id": 23243
}
]
}
我已经可以返回popularity的值了
例如,我需要知道如何访问name 的值以及它是哪个name?
我也尝试将其转换为数组,但没有成功或我做错了什么。
电影类:
public class Movie {
public string popularity {get; set;}
public object production_companies {get; set;}
public Movie GetBasic(string id) {
string json = @"{
"popularity": 3.518962,
"production_companies": [
{
"name": "value1",
"id": 4
},
{
"name": "value2",
"id": 562
},
{
"name": "value13",
"id": 14654
},
{
"name": "value4",
"id": 19177
},
{
"name": "value5",
"id": 23243
}
]
}";
Movie Data = JsonConvert.DeserializeObject<Movie>(json);
return Data;
}
到目前为止我做了什么:
@{
var arr = Item.production_companies.ToString().Substring(1, (Item.production_companies.ToString().Length - 2)).ToArray();
foreach(var a in arr) {
@a.name
}
}
【问题讨论】:
-
你能告诉我们一些你试过的代码吗?也请在问题本身中添加。
-
你在使用解析器吗? JSON.net 之类的?
-
@Wickramaranga 是的,我正在使用 JSON.NET
-
映射到 json 对象的类在哪里?您可以在json2csharp.com 在线转换。
标签: c# json object indexing key-value