【发布时间】:2018-12-10 20:12:18
【问题描述】:
我试图从我从 Web 服务器获得的响应中提取路由号。响应如下所示;
[{"Description":"METRO Blue Line","ProviderID":"8","Route":"901"},{"Description":"METRO Green Line","ProviderID":"8" ,"路线":"902"},
我只需要获取路线编号,以便我可以用它们填充组合框。我正在尝试使用循环,因为有很多。我当前的解决方案获得了第一个路由号码,但由于某种原因,我只获得了之后的提供商号码。这就是我到目前为止所拥有的。
//get bus routes and popluate the busRoutecmb
restClient.endPoint = routeUrl + formatLine;
string response = restClient.request();
//show me what was returned for debug puposes
System.Diagnostics.Debug.Write(response);
//sort through data and put relevent item in a list
List<string> responseItems = new List<string>();
//splitting into routes
string[] splitByRoute = response.Split('}');
//extracting route number from elements in splitByRoute
List<string> extractedRouteNums = new List<string>();
foreach (string thing in splitByRoute)
{
//splitting each bus route up by piece of information
string[] splitByWord = thing.Split(',');
//getting rid of everything but the route number
int length = splitByWord.Length;
int count = 2;
while (count <= length)
{
string[] word = splitByWord[count].Split(':');
string routeNum = word[1].Trim('"');
count += 3;
extractedRouteNums.Add(routeNum);
System.Diagnostics.Debug.WriteLine(count);
System.Diagnostics.Debug.WriteLine(routeNum);
}
}
//add repsonse to busRoutecmb
busRoutecmb.DataSource = extractedRouteNums;
}
【问题讨论】:
-
为什么不用 JSON.NET 来反序列化 JSON 字符串呢?您不需要使用字符串操作来解析它
标签: c# .net json string-formatting