After several test and search, I can't find a way to navigate after parsing a JSON; here is the post-parsing result :
经过多次测试和搜索,我在解析JSON之后找不到导航的方法;这是解析后的结果:
Object {documentation: "https://geocoder.opencagedata.com/api", licenses: Array[2], rate: Object, results: Array[1], status: Object…}
documentation : "https://geocoder.opencagedata.com/api" licenses : Array[2] rate : Object results : Array[1] 0 : Object annotations : Object components : Object building : "C" city : "Bordeaux" country : "France" country_code : "fr" county : "Bordeaux" postcode : "33000" road : "Quai de Bacalan" state : "Aquitaine" suburb : "Bordeaux Maritime"
文档:“https://geocoder.opencagedata.com/api”licenses:Array [2] rate:Object results:Array [1] 0:Object annotations:Object components:Object building:“C”city:“Bordeaux”country :“法国”country_code:“fr”县:“波尔多”邮政编码:“33000”道路:“Quai de Bacalan”州:“阿基坦”郊区:“波尔多海事”
For example I can get the value of the response with the following code :
例如,我可以使用以下代码获取响应的值:
var locname = response.status.code;
But in the case there is a int as Object, like this :
但是在这种情况下有一个int作为Object,如下所示:
var locname = response.results.0.formatted;
I have the following error :
我有以下错误:
Uncaught SyntaxError: Unexpected number
未捕获的SyntaxError:意外的数字
I try to escape the character, putting quote, etc but I couldn't find any solution.
我试图逃避角色,引用等等,但我找不到任何解决方案。
2 个解决方案
#1
0
Since results is an array you must use the following syntax:
由于结果是数组,因此必须使用以下语法:
var locname = response.results[0].formatted;
Instead of
var locname = response.results.0.formatted;
#2
1
in javascript, an object is also accessible as array, so for example, you have an object like this:
在javascript中,一个对象也可以作为数组访问,例如,你有一个像这样的对象:
var obj = {name: 'Hans Yulian', age: 21, message: 'Handsome'};
obj[0] = 'This is an number index';
obj['message'] = 'Too Handsome';
all the key is accepted as long as they aren't something that contain special characters (-+=! etc) and not started with number. in any case you have the field that don't satisfy this exception, then you have to access it in array-way. you can assign the value the same way as array, and also to get the content of that field.
所有密钥都被接受,只要它们不是包含特殊字符的东西( - + =!等)而不是以数字开头。在任何情况下,您都有不满足此异常的字段,那么您必须以数组方式访问它。您可以使用与数组相同的方式分配值,也可以获取该字段的内容。
in case you need to access something like
如果你需要访问类似的东西
var locname = response.results.0.formatted;
Then the thing that you need is
然后你需要的是
var locname = response.results[0].formatted;
you can try make a html file with this content
你可以试着用这个内容制作一个html文件
<script>
var obj = {name: 'Hans Yulian', age: 21, message: 'Handsome'};
obj[0] = 'This is an number index';
obj['message'] = 'Too Handsome';
obj[1] = {};
obj[1].name = 'Handsome';
obj.handsome = [];
obj.handsome[0] = {};
obj.handsome[0].hansyulian = 'So Handsome';
console.log(obj);
console.log(obj[1].name);
console.log(obj.handsome[0].hansyulian);
</script>
and try see the console(right click, inspect element, select console for google chrome) to understand what happens there
并尝试查看控制台(右键单击,检查元素,选择谷歌浏览器的控制台)以了解那里发生的事情
#1
0
Since results is an array you must use the following syntax:
由于结果是数组,因此必须使用以下语法:
var locname = response.results[0].formatted;
Instead of
var locname = response.results.0.formatted;
#2
1
in javascript, an object is also accessible as array, so for example, you have an object like this:
在javascript中,一个对象也可以作为数组访问,例如,你有一个像这样的对象:
var obj = {name: 'Hans Yulian', age: 21, message: 'Handsome'};
obj[0] = 'This is an number index';
obj['message'] = 'Too Handsome';
all the key is accepted as long as they aren't something that contain special characters (-+=! etc) and not started with number. in any case you have the field that don't satisfy this exception, then you have to access it in array-way. you can assign the value the same way as array, and also to get the content of that field.
所有密钥都被接受,只要它们不是包含特殊字符的东西( - + =!等)而不是以数字开头。在任何情况下,您都有不满足此异常的字段,那么您必须以数组方式访问它。您可以使用与数组相同的方式分配值,也可以获取该字段的内容。
in case you need to access something like
如果你需要访问类似的东西
var locname = response.results.0.formatted;
Then the thing that you need is
然后你需要的是
var locname = response.results[0].formatted;
you can try make a html file with this content
你可以试着用这个内容制作一个html文件
<script>
var obj = {name: 'Hans Yulian', age: 21, message: 'Handsome'};
obj[0] = 'This is an number index';
obj['message'] = 'Too Handsome';
obj[1] = {};
obj[1].name = 'Handsome';
obj.handsome = [];
obj.handsome[0] = {};
obj.handsome[0].hansyulian = 'So Handsome';
console.log(obj);
console.log(obj[1].name);
console.log(obj.handsome[0].hansyulian);
</script>
and try see the console(right click, inspect element, select console for google chrome) to understand what happens there
并尝试查看控制台(右键单击,检查元素,选择谷歌浏览器的控制台)以了解那里发生的事情