2012-06-01 34 views
2

我有一段时间从下面的JSON返回访问数值距离“值”。我使用JSON.NET 3.5库。程序死亡:使用C访问JSON数据#

编译器错误消息: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; 

    } 

回答

1

元素是数组中的JSON,但单一的价值在你的类,使它阵列应该工作:

public GeoElements[] elements { get; set; } 
+0

清除了一个错误并留下了我:CS1061:'System.Array'不包含'distance'的定义,并且没有找到接受'System.Array'类型的第一个参数的扩展方法'distance'( ?是否缺少使用指令或程序集引用) 源错误: 61行:(INT I = 0; I <计数;我++) 线62:{ 线63:DIST = geoResponse。行[I] .elements.distance.value; 第64行:// dist = dist/1609.3; 行65:} – user1431356

+0

也许它应该是列表,而不是一个数组...我没有JSON.net安装程序来尝试它... –

1

The deserialized type must be an array or implement a collection interface like IEnumerable, ICollection or IList.

这也正是它在锡说:你需要的GeoElements数组反序列化您的JSON。 您的代码中的GeoElement是单个值。它是你的json中的一个数组。因此,无论继承IList/ICollection接口在GeoElement类,或者声明您GeoElement作为GeoElements

class GeoElement : ICollection { ... } 

GeoElement[] elements = JSONConverter.Deserialize(stream); 
+0

我无法将头围绕ICollection。我添加了一个嵌套的循环来获取元素数组的内容,但这是错误的。我相信这是因为我没有正确引用与数组相关的元素数组,但我不确定我错过了什么。我会说LINQ和JSON结构对我来说是全新的 – user1431356

+0

最后发现了这个问题。我的类被定义为错误的。找到http://json2csharp.com/我需要做的就是将我从Google获得的JSON数据放入框中,并为我生成适当的类结构。 – user1431356

1

阵列发现的问题终于。我的类被定义为错误的。找到http://json2csharp.com/我所需要做的就是将我从Google获得的JSON数据放入框中,并为我生成适当的类结构。