2

我有一段时间从下面的 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;

    }
4

3 回答 3

1

终于找到问题了。我的课程定义错误。找到http://json2csharp.com/我需要做的就是将我从 Google 获得的 JSON 数据放入框中,它为我生成了正确的类结构。

于 2012-06-26T21:04:17.233 回答
1

Elements is array in JSON, but single value in your classes, making it array should work:

public GeoElements[] elements { get; set; } 
于 2012-06-01T21:17:02.273 回答
1

反序列化的类型必须是数组或实现集合接口,如 IEnumerable、ICollection 或 IList。

它完全按照它在锡上所说的:你需要一个数组GeoElements来反序列化你的 JSON。你GeoElement在你的代码中是一个单一的值。它是您的 json 中的一个数组。因此,要么继承类中的IList/ICollection接口GeoElement,要么将 GeoElement 声明为 GeoElements 数组

class GeoElement : ICollection { ... }

或者

GeoElement[] elements = JSONConverter.Deserialize(stream);
于 2012-06-01T21:37:24.873 回答