I read more here, but I have not found the answer to my problem My problem:
I have (from php via ajax return json string)
str = '[{"id":"159","pavadinimas":"Building B","_id":"200",.................}]';
this is JSON string and I do obj = $.parseJSON(str);
i can take any value in that way like alert(obj[0]._id) I get 200.
But how I can get associative array from json like:
dataArray = [
"id":"159",
"pavadinimas":"Building B",
"_id":"200",
...
]
I want get value 200 like that
val = dataArray['_id'];
There is no associative array concept in javascript, but only object.
var arr = $.parseJSON(str);
var obj = arr[0];
var _id = obj['_id']
Javascript does not have associative arrays. You will have to use objects, which use curly brackets {} instead.
dataObj = {
"id":"159",
"pavadinimas":"Building B",
"_id":"200",
...
}
console.log( dataobj.id );
console.log( dataobj['id'] );
Both works the same.
So for your example, you can access the object the way you would expect an associative array to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With