【发布时间】:2016-09-21 20:10:06
【问题描述】:
这是我的 json 数据:
{
"date": "2016-08-26",
"time_of_day": "14:19",
"request_time": "2016-08-26T14:19:59+01:00",
"station_name": "Derby",
"station_code": "DBY",
"departures": {
"all": [
{
"service": "22152000",
},
{
"service": "22150000",
},
{
"service": "22180008",
}
]
}
}
我要做的是将上面 json 中的每个服务变量放入一个列表中。在此之后,我想通过 api 链接分别调用这些服务变量中的每一个。这是我的 C# 代码:
JArray items = new JArray();
items.Add(service["service"]);
int serviceLength = items.Count;
for (int i = 0; i < items.Count; i++)
{
string moreJson = get_web_content("http://transportapi.com/v3/uk/train/service/" + items[i] + "/" + date + "/" + hours + ":" + minutes + "/timetable.json?" + appID + "/" + appKey);
dynamic schedluedContent = JsonConvert.DeserializeObject(moreJson);
JArray items2 = new JArray();
foreach (JObject stops in schedluedContent.stops)
{
dr = dt.NewRow();
dr["Station Code"] = stops["station_code"];
dr["Station Name"] = stops["station_name"];
dr["Aimed Arrival Time"] = stops["aimed_arrival_time"];
dt.Rows.Add(dr);
GridViewTrainTimes.DataSource = dt;
GridViewTrainTimes.DataBind();
}
}
感谢您的帮助!
【问题讨论】:
-
如果您将 json 转换为模型 json2csharp.com,您可以轻松完成
-
我不知道从哪里开始,但我会调查一下谢谢!
-
您尝试执行的操作似乎与数据不匹配。在您的源 json 中没有提及
aimed_arrival_time或stop。stops应该是离开吗? -
@Ceilingfish - 我认为显示的 JSON 代表一个更大的 json 对象中的单个条目,看起来像
{ "stops": [ { ... the object in the question ...} ] }
标签: c# asp.net json api json.net