The webpage I'm developing retrieves (product) information from the database via an ajax call to a php file. Given the array structure and related json encoded nested string in the simplified php file below, how to define the corresponding nested array in javascript in an elegant way?
I looked at the examples such as in JS nested arrays, but still get stuck...
php code:
$productinfo = array();
$productinfo['supplierA']['agreementX']['productY']['productpropertyZ'] = 'valueProductproperty';
echo json_encode($productinfo);
1 Answer 1
you are trying to create an object, not an array, Arrays are ordered lists, objects are unordered key/value pairs.
This would do the job:
var obj = {"supplierA": {"agreementX": {"productY": {"productpropertyZ":"valueProductproperty"}}}};
with more than one value, this could look like this:
var obj = {
"A": {
"1": "asd",
"2": {
"I": "asdf",
"II": "asdfg"
}
},
"B": "asdfgh"
}
for more info just go to http://json.org/
4 Comments
TypeError: Cannot read property 'agreementX' of undefinedExplore related questions
See similar questions with these tags.
;at the end of the first line.