1. Create an initial JSON object with a single element and an array:
// {$id} is a PHP variable but it can be anything else
jsonObj = {"id" : "{$id}", "myArray": []};
2. Construct the JSON object
item = {}
item ["id"] = "some Id";
item ["value"] = "Some Value";
jsonObj.myArray.push(item);
3. Output JavaScript:
Object {id: "564f48ba-e9d5-463a-2325-58f91c653aa8", myArray: Array(1)}
where myArray is broken down as follows:
myArray: Array(3)
0: Object
id: "Some Id",
value: "Some Value"
4. Output PHP:
Array
(
[id] => 564f48ba-e9d5-463a-2325-58f91c653aa8
[myArray] => Array
(
[0] => Array
(
[id] => Some Id
[value] => Some Value
)
)
)
Wednesday, April 26, 2017
JavaScript: Constructing a Simple JSON Object Dynamically
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment