1

After several test and search, I can't find a way to navigate after parsing a JSON; here is the post-parsing result :

    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"

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 :

var locname = response.results.0.formatted;

I have the following error :

Uncaught SyntaxError: Unexpected number

I try to escape the character, putting quote, etc but I couldn't find any solution.

2 Answers 2

1

in javascript, an object is also accessible as array, so for example, you have an object like this:

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

<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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Hans, for taking time to answer me; I will go into this in depth with your example !
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 Comments

Thank you very much, this works perfectly; I'm such a noob !
@bubu great, then pls mark the question as answered :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.