I’m having trouble creating a JSON structure as javascript objects. Basically I have a JSON object coming into my javascript program then the program updates it and the object ships back out.
lets say I import this JSON structure:
{
"123":{
itemname: "name",
itemprice: "price"
}
"456":{
itemname: "name2",
itemprice: "price2"
}
}
And I want this output:
{
"123":{
itemname: "name",
itemprice: "price"
}
"456":{
itemname: "name2",
itemprice: "price2"
}
"789":{
itemname: "name3",
itemprice: "price3"
}
}
I would use the JSON.parse on input and JSON.stringify on output.
If in PHP, $json contained this structure decoded, then I could add my entry via:
$json['789']['itemname']='name3';
$json['789']['itemprice']='price3';
So in Javascript, I tried this (assume x is the structure decoded via JSON.parse):
var i="789",j="itemname";
if(!x[i]){x[i]={}}
if(!x[i][j]){x[i][j]="";}
x[i][j]+=n;
and somewhere in that declaration my web browser locks up and the javascript console does not react. The values of i and j are set to fixed values as an example, but the values can change in the real program.
What am I doing wrong?