如何从JavaScript中获取json中的特定数据?

时间:2018-02-02 15:50:29

标签: javascript json google-maps

我想从谷歌地理编码API的结果获得纬度和经度,但我不知道如何使用json取出纬度和经度。谷歌地理编码的结果如下:

[{
'address_components': [{
    'long_name': 'Macau',
    'short_name': 'MO',
    'types': ['country', 'locality', 'political']
}],
'formatted_address': 'Macau',
'geometry': {
    'bounds': {
        'northeast': {
            'lat': 22.2170639,
            'lng': 113.6127001
        },
        'southwest': {
            'lat': 22.1066001,
            'lng': 113.5276053
        }
    },
    'location': {
        'lat': 22.198745,
        'lng': 113.543873
    },
    'location_type': 'APPROXIMATE',
    'viewport': {
        'northeast': {
            'lat': 22.2170639,
            'lng': 113.6127001
        },
        'southwest': {
            'lat': 22.1066001,
            'lng': 113.5276053
        }
    }
},
'place_id': 'ChIJ88g14uB6ATQR9qyFtCzje8Y',
'types': ['country', 'locality', 'political']
}]

我只想使用:

'location': {
        'lat': 22.198745,
        'lng': 113.543873
    },

并将它们保存在两个变量中。我该怎么做?

2 个答案:

答案 0 :(得分:1)

将您的JSON复制到http://jsonviewer.stack.hu/的文本'选项卡并查看“查看器”选项卡。

您将看到JSON字符串的树视图。

对于位置,它是:

var obj = // your whole json object
var obj = JSON.parse(jsonString) // if you have JSON string not the object. 

然后

var location = obj[0].geometry.location;

这里有一个数组,所以首先,我们访问索引= 0的第一个元素,obj[0]然后访问几何obj[0].geometry,然后访问位置obj[0].geometry.location

JSON基于两种结构:

  • 名称/值对的集合。在各种语言中,这被实现为对象,记录,结构,字典,哈希表,键控列表或关联数组。
  • 有序的值列表。在大多数语言中,这被实现为数组,向量,列表或序列。

Read More

  • []表示数组。
  • {}表示对象。 (关键:价值对)



var a = ['a', 'b', {'c': 'i am value of key c', 'd': 'i am value of key d'}, 10];

console.log('Array element: a[0] = ', a[0]);
console.log('Access inside property of object: a[2].c = ', a[2].c);
console.log('Another way of accessing property: a[2][\'c\'] = ', a[2]['c']);
console.log('Accessing with . notation seems easy: a[2].d = ', a[2].d);
console.log('Array element can be string, number, obj, array, etc: a[3] = ', a[3]);




请查看您的示例:



var a = [  
    {  
        'address_components':[  
            {  
                'long_name':'Macau',
                'short_name':'MO',
                'types':[  
                    'country',
                    'locality',
                    'political'
                ]
            }
        ],
        'formatted_address':'Macau',
        'geometry':{  
            'bounds':{  
                'northeast':{  
                    'lat':22.2170639,
                    'lng':113.6127001
                },
                'southwest':{  
                    'lat':22.1066001,
                    'lng':113.5276053
                }
            },
            'location':{  
                'lat':22.198745,
                'lng':113.543873
            },
            'location_type':'APPROXIMATE',
            'viewport':{  
                'northeast':{  
                    'lat':22.2170639,
                    'lng':113.6127001
                },
                'southwest':{  
                    'lat':22.1066001,
                    'lng':113.5276053
                }
            }
        },
        'place_id':'ChIJ88g14uB6ATQR9qyFtCzje8Y',
        'types':[  
            'country',
            'locality',
            'political'
        ]
    }
];

// way 1
var locObj1 = a[0]['geometry']['location'];
// way 2
var locObj2 = a[0].geometry.location;

console.log("Way1: a[0]['geometry']['location'] = ", locObj1);
console.log("Way2: a[0].geometry.location = ", locObj1);




答案 1 :(得分:0)

您可以通过以下对象访问:

// set variables
int numberOfRankedItems;
if (queryData.NumberOfRankedItems != null)
{
    numberOfRankedItems = (int) queryData.NumberOfRankedItems;
} else
{
    numberOfRankedItems = 0;
}
string minMaxChoice = queryData.MinMaxChoice;
string minMaxFeatureColumnName = queryData.MinMaxFeatureColumnName;

string orderByPrompt = "";
if (minMaxChoice == "max")
{
    orderByPrompt = "it.Sum(POSITIONVALUE) descending";
} else if (minMaxChoice == "min")
{
    orderByPrompt = "it.Sum(POSITIONVALUE) ascending";
}

string groupByPrompt = queryData.MinMaxFeatureColumnName;

// execute query
IQueryable query = (from tableRow in Context.xxx
                        where /* some filters */
                        select tableRow).
                        GroupBy(groupByPrompt, "it").
                        OrderBy(orderByPrompt).
                        Select("new (it.Key as minMaxFeature, it.Sum(POSITIONVALUE) as sumPosition)").
                        Take(numberOfRankedItems);

// create response dictionary
Dictionary<int?, Dictionary<string, int?>> response = new Dictionary<int?, Dictionary<string, int?>>();
int count = 1;
foreach (dynamic item in query)
{
    string minMaxFeatureReturn = item.GetType().GetProperty("minMaxFeature").GetValue(item);
    int? sumPositionReturn = item.GetType().GetProperty("sumPosition").GetValue(item);
    response[count] = new Dictionary<string, int?>() { { minMaxFeatureReturn, sumPositionReturn } };
    count++;
}            
return response;

然后

const json = JSON.parse("<your_json>"); // your json between ""