我有一段时间从下面的 JSON 返回中访问数字距离“值”。我正在使用 JSON.NET 3.5 库。该程序因以下原因而死:
编译器错误消息:CS0119:“System.Xml.Linq.Extensions.Elements(System.Collections.Generic.IEnumerable, System.Xml.Linq.XName)”是一个“方法”,在给定的上下文中无效
源错误:
Line 64: GeoElements Elements = JsonConvert.DeserializeObject<GeoElements>geoResponse.ToString());
Line 65:
Line 66: count2 = geoResponse.Elements.Length;
Line 67: for (int j = 0; j < count2; j++)
Line 68: {
这是 JSON 返回,后面是 C# 代码隐藏:
{
"destination_addresses" : [ "3095 E Patrick Ln, Las Vegas, NV 89120, USA" ],
"origin_addresses" : [ "Vegas @ Advanced Tech Academy (E), Las Vegas, NV 89106, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "13.4 mi",
"value" : 21573
},
"duration" : {
"text" : "24 mins",
"value" : 1429
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
public partial class maproute : System.Web.UI.Page
{
public class GeoDistance
{
public string value { get; set; }
public string text { get; set; }
}
public class GeoElements
{
public GeoDistance distance { get; set; }
}
public class GeoResult
{
public GeoElements[] Elements { get; set; }
}
public class GeoResponse
{
public string Status { get; set; }
public GeoResult[] Rows { get; set; }
}
public int parseDistance(string stream)
{
//process response file
GeoResponse geoResponse = JsonConvert.DeserializeObject<GeoResponse>(stream);
int dist = 0;
int count = 0;
int count2 = 0;
try
{
if (geoResponse.Status == "OK")
{
count = geoResponse.Rows.Length;
for (int i = 0; i < count; i++)
{
GeoElements Elements = JsonConvert.DeserializeObject<GeoElements>(geoResponse.ToString());
count2 = geoResponse.Rows[i].Elements.Length;
for (int j = 0; j < count2; j++)
{
dist = geoResponse.Rows[i].Elements[j].distance.value;
//dist = dist/1609.3;
}
//dist = dist/1609.3;
}
}
// dist = 4;
}
catch(Exception ex)
{
Response.Write("DEBUG: ");
Response.Write("Status: " + geoResponse.Status);
Response.Write("</br>");
Response.Write("Results: " + geoResponse.Rows.Length);
Response.Write("</br>");
}
return dist;
}