I'm a newbie in jQuery and JSON. I hope you can help me with my little task. I have the following JSON object structure:
{
"recipe": {
"myREAL":0,
"mySTRING":"STRING variable",
"myUDINT":0,
"mybool":0,
"ingNumber":[0,0,0,0,0],
"ingMinimumWeight":[0,0,0,0,0],
"ingNominalWeight":[0,0,0,0,0]
}
}
I would like to get the name of the array name instead of only getting the index and the value of the index.
My output so far:
Key: myREAL Value: 0
Key: mySTRING Value: STRING variable
Key: myUDINT Value: 0
Key: mybool Value: 0
Key: 0 Value: 0
Key: 1 Value: 0
Key: 2 Value: 0
Key: 3 Value: 0
Key: 4 Value: 0
...
I'm interested in such structure:
Key: myREAL Value: 0
Key: mySTRING Value: STRING variable
Key: myUDINT Value: 0
Key: mybool Value: 0
ingNumber:
Key: 0 Value: 0
Key: 1 Value: 0
Key: 2 Value: 0
Key: 3 Value: 0
Key: 4 Value: 0
ingMinimumWeight:
...
I would like to do it in Jquery without specifiying the name of the array variable.
My jQuery code:
function iter(obj) {
for (var key in obj) {
if (typeof (obj[key]) == 'object') {
iter(obj[key]);
} else {
content = " Key: " + key + " Value: " + obj[key] + '\n';
var box = $("#myArea");
box.val(box.val() + content);
}
}
}
As per your desired output, you need to add content to box in before recursively calling the function again.
function iter(obj) {
var box = $("#myArea");
for (var key in obj) {
if (typeof (obj[key]) == 'object') {
content = key+ " : \n";
box.val(box.val() + content);
iter(obj[key]);
} else {
content = " Key: " + key + " Value: " + obj[key] + '\n';
box.val(box.val() + content);
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加