For Example
I have Object named tempobj
and below are tempobj[0]
and tempobj[1]
sample data.
I want to add extra info like name and status this object
tempobj ["info"]["name"] = "title";
tempobj ["info"]["id"] = "23243";
But when i do stringify
, Jquery is ignoring this value...how can i add data like this to this kind of structure.
[
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
-
1Can you show us more of your code?bhspencer– bhspencer2015年02月05日 15:09:38 +00:00Commented Feb 5, 2015 at 15:09
-
try tempobj["info"] = {"name":"title"} if you want to add the info to the tempobj but if you want to add it a a specific index, tempobj[iIndex]["info"] = {"name":"title"}satchcoder– satchcoder2015年02月05日 15:15:22 +00:00Commented Feb 5, 2015 at 15:15
2 Answers 2
tempobj
is an array ([]
).
When you try to set some values with:
tempobj["info"]["name"] = "title";
tempobj["info"]["id"] = "23243";
you treat it like an object ({}
).
If you want to add some data to your tempobj
, you have to change its structure like this for example:
{
"info": {
"name": "title",
"id": "23243"
},
"items": [
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
]
}
Here is a sample:
var tempobj = {
items: [
[{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
}, {
"name": "snack ",
"value": "pecan pie butter",
"check": 0
}, {
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}],
[{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
}, {
"name": "snack ",
"value": "Smoked Salmon nori roll "
}, {
"name": "dessert",
"value": "Apple muffins"
}]
]
}
tempobj.info = {
name: 'title',
id: '23243'
};
// Another way:
//tempobj.info = {};
//tempobj.info.name = 'title';
//tempobj.info.id = '23243';
console.log(JSON.stringify(tempobj));
-
can you post sample js code to achieve this structure..I am getting json parse errorVishnu– Vishnu2015年02月05日 15:36:09 +00:00Commented Feb 5, 2015 at 15:36
-
I added a sample, and I modified the first structure to be JSON compliant (this was a javascript object before).Gnucki– Gnucki2015年02月05日 16:04:21 +00:00Commented Feb 5, 2015 at 16:04
-
Glad if I could help you!Gnucki– Gnucki2015年02月05日 20:23:57 +00:00Commented Feb 5, 2015 at 20:23
It sounds like you're doing something like this:
// Create an array
var a = [];
// Add an entry to it
a[0] = "I'm an array entry";
// Add a non-entry property to it
a.foo = "bar";
// Convert to JSON
var json = JSON.stringify(a);
...and finding that the string doesn't have the foo
property.
That's correct. JSON's arrays don't support arbitrary non-entry properties the way JavaScript's arrays do, and so they get silently dropped during serialization (the same way properties with the value undefined
or referring to functions do).
The answer is not to do that if you need to go through JSON.
But, the structure you quoted later in your question doesn't do that, and can readily be created like this:
var data = [
[
{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
},
{
"name": "snack ",
"value": "pecan pie butter",
"check": 0
},
{
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
}
],
[
{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
},
{
"name": "snack ",
"value": "Smoked Salmon nori roll "
},
{
"name": "dessert",
"value": "Apple muffins"
}
]
];
var json = JSON.stringify(data);
That structure is (from the outside in): An array containing two entries, each of which is another array; within each array, you have a series of objects, which have properties.
I don't know why you want to have the two arrays inside the outermost array, but that's what the structure showed.
If you meant just a single array, that would look like this if you were creating it all at once:
var data = [
{
"name": "Breakfast",
"value": "buffalo strip ",
"check": 0
},
{
"name": "snack ",
"value": "pecan pie butter",
"check": 0
},
{
"name": "dessert",
"value": "chocolate zucchani brownie",
"check": 0
},
{
"name": "Breakfast",
"value": "Stir Fried Kale and Baccon ",
"check": 1
},
{
"name": "snack ",
"value": "Smoked Salmon nori roll "
},
{
"name": "dessert",
"value": "Apple muffins"
}
];
var json = JSON.stringify(data);
...or if building it up over time:
var data = [];
data.push({
name: "Breakfast",
value: "buffalo strip ",
check: 0
});
data.push({
name: "snack ",
value: "pecan pie butter",
check: 0
});
// ...
var json = JSON.stringify(data);
-
why did you convert this to single array ? I want it to be 2Vishnu– Vishnu2015年02月05日 15:35:27 +00:00Commented Feb 5, 2015 at 15:35
-
@Vishnu: Because it wasn't at all clear how or why they were two. The question lacks clarity throughout. The above shows all the fundamentals you need, if you need two arrays, adjust accordingly.T.J. Crowder– T.J. Crowder2015年02月05日 15:38:31 +00:00Commented Feb 5, 2015 at 15:38