2012-06-01 28 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數據放入框中,併爲我生成適當的類結構。