0

所以我想变成JSON的代码如下

var locationData = [];
locationData['lat'] = position.coords.latitude;
locationData['long'] = position.coords.longitude;
locationData['address']['road'] = data['address']['road'];
locationData['address']['suburb'] = data['address']['suburb'];
locationData['address']['city'] = data['address']['city'];
locationData['address']['county'] = data['address']['county'];
locationData['address']['state'] = data['address']['state'];
locationData['address']['postcode'] = data['address']['postcode'];
locationData['address']['country'] = data['address']['country'];
locationData['address']['country_code'] = data['address']['country_code'];

但是当我运行它时,它并没有提醒我它已经成功

4

2 回答 2

5

您最初的声明是错误的:

var locationData = [];

这会创建一个数组,它不会像您期望的那样工作;你想要一个对象。您还需要初始化内部任何复杂的数据结构,例如address

var locationData = {
    address: {}
}

边注

数组也是 JavaScript 中的对象,但在使用数字属性时它的行为方式很特殊。在您的情况下,您根本没有使用数字属性,这就是为什么常规对象更适合您的需求。

于 2012-06-29T02:37:46.543 回答
0

正如您将 locationData 声明为数组一样,您也需要将 locationData['address'] 声明为数组。(locationData['address']=[]在你的第三或第四行)。

于 2012-06-29T02:33:47.443 回答