c#解析json字符串数组_使用json.NET和C#解析JSON数组

我有一些我需要解析的以下

JSON格式的数据:

{

"status":0,

"timestamp":"8:20pm",

"routes":[

{

"directions":[

"E Towne",

"ETP"

],

"routeID":"30"

},

{

"directions":[

"Johnson",

"Observatory"

],

"routeID":"81"

}

]

}

使用json.net,我想得到以下输出:

30 E Towne – ETP

81 Johnson – Observatory

使用下面的代码,我得到以下不正确的输出:

30 E Towne – ETP

81 E Towne – ETP

如何将方向数组项写入相应的routeID项?我的代码:

public class Route

{

public string routeID { get; set; }

public string directions { get; set; }

}

private void routeClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

{

string jsonResults_routes = e.Result;

JObject routeData = JObject.Parse(jsonResults_routes);

JArray routeIdArray = (JArray)routeData["routes"];

JArray routeDirections = (JArray)routeData["routes"][0]["directions"];

List l = new List();

for (int i = 0; i < routeIdArray.Count; i++)

{

Route BusRoutes = new Route();

JObject routeIDarrayObject = (JObject)routeIdArray[i];

BusRoutes.routeID = (string)routeIDarrayObject["routeID"];

string sep = " - ";

string bothDirections = String.Join(sep, routeDirections);

List sc = new List();

string[] direction = new string[]{bothDirections};

sc.AddRange(direction);

foreach (string direx in sc)

{

BusRoutes.directions = direx;

}

l.Add(BusRoutes);

}

var newList = l.OrderBy(x => x.routeID).ToList();

lbRoutesData.ItemsSource = newList;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值