c# - 使用 C# 访问 JSON 数据

标签 c# google-maps-api-3 json.net

我很难从下面的 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;

    }

最佳答案

Elements 是 JSON 中的数组,但在类中是单个值,使其成为数组应该可以工作:

public GeoElements[] elements { get; set; } 

关于c# - 使用 C# 访问 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10857424/

相关文章:

c# - 三位数字的递归排列

javascript - 显示没有重复的世界地图

javascript - Google Javascript API Photo getURL 返回临时 URL 并且无法找到 photo_reference

c# - .NET 对象最灵活的序列化是什么,但实现起来很简单?

c# - 遵循 EnumMemberAnnotation 的 Enum.ToString()

c# - FluentValidation 中依赖规则的顺序

c# - 从不带 .ashx 扩展名的 URL 访问 ASHX

c# - Visual Studio - 自定义文档选项卡栏分组

android - 从 android 中的谷歌地图中删除谷歌文本

遵循 Ruby 属性命名约定的 C# JSON.NET 约定?