admin管理员组文章数量:1303374
Consider the following nested JSON:
{
"state": [
"Tennessee"
],
"more_data": [
{
"letters": {
"last": "e",
"first": "T"
}
}
]
}
I want to print the JSON in JavaScript in a flat manner, i.e. root_key=value:
var my_json_str = "{\"state\":[\"Tennessee\"],\"more_data\":[{\"letters\":{\"first\":\"T\",\"last\":\"e\"}}]}";
console.log(my_json_str);
my_json = jQuery.parseJSON(my_json_str);
for (var key in my_json) {
console.log(key,":",my_json[key]);
}
But I get (FireBug console):
state : ["Tennessee"]
more_data : [Object { letters={...}}]
Instead of the desired:
state:["Tennessee"]
more_data:[{"letters":{"first":"T","last":"e"}}]
How do I fix this?
Solution - following your answers:
/
var jsonStr = "{\"state\":[\"Tennessee\"],\"more_data\":[{\"letters\":{\"first\":\"T\",\"last\":\"e\"}}]}";
var jsonObj = JSON.parse(jsonStr);
for (key in jsonObj) {
console.log(key+':'+JSON.stringify(jsonObj[key]));
}
Which gives:
state:"Tennessee"
more_data:{"letters":{"first":"T","last":"e"}}
Consider the following nested JSON:
{
"state": [
"Tennessee"
],
"more_data": [
{
"letters": {
"last": "e",
"first": "T"
}
}
]
}
I want to print the JSON in JavaScript in a flat manner, i.e. root_key=value:
var my_json_str = "{\"state\":[\"Tennessee\"],\"more_data\":[{\"letters\":{\"first\":\"T\",\"last\":\"e\"}}]}";
console.log(my_json_str);
my_json = jQuery.parseJSON(my_json_str);
for (var key in my_json) {
console.log(key,":",my_json[key]);
}
But I get (FireBug console):
state : ["Tennessee"]
more_data : [Object { letters={...}}]
Instead of the desired:
state:["Tennessee"]
more_data:[{"letters":{"first":"T","last":"e"}}]
How do I fix this?
Solution - following your answers:
http://jsfiddle/wrAUB/
var jsonStr = "{\"state\":[\"Tennessee\"],\"more_data\":[{\"letters\":{\"first\":\"T\",\"last\":\"e\"}}]}";
var jsonObj = JSON.parse(jsonStr);
for (key in jsonObj) {
console.log(key+':'+JSON.stringify(jsonObj[key]));
}
Which gives:
state:"Tennessee"
more_data:{"letters":{"first":"T","last":"e"}}
Share
Improve this question
edited Mar 27, 2012 at 15:38
Adam Matan
asked Mar 27, 2012 at 15:15
Adam MatanAdam Matan
136k155 gold badges412 silver badges580 bronze badges
5
-
3
You don't have to fix anything, there is nothing wrong (well, unless you really want to print it in a different way, but why?). It's just how Firebug displays nested objects. You can work with the data as expected. Btw, you don't have JSON within JSON. You just have JSON (JSON within JSON would be something like
{"foo": "{\"bar\": 42}"}
, i.e. a string containing JSON). – Felix Kling Commented Mar 27, 2012 at 15:17 - This is a printing problem, not a parsing problem. – Matt Ball Commented Mar 27, 2012 at 15:17
- Reminds me of the old infomercials...: "Sorry, Tennessee." – lance Commented Mar 27, 2012 at 15:17
- I believe you can use the method suggested here for flattening your object – J. Ed Commented Mar 27, 2012 at 15:18
-
1
Just a side note, but if you are fetching the JSON with ajax, you can use
$.getJSON()
and this will eliminate the need to use$.parseJSON()
api.jquery./jQuery.getJSON – Dutchie432 Commented Mar 27, 2012 at 15:20
2 Answers
Reset to default 5You can use JSON.stringify
to turn the objects you're iterating over back into JSON strings:
var jsonStr = "{\"state\":[\"Tennessee\"],\"more_data\":[{\"letters\":{\"first\":\"T\",\"last\":\"e\"}}]}";
var jsonObj = JSON.parse(jsonStr);
for (key in jsonObj) {
console.log(key+':'+JSON.stringify(jsonObj[key]));
}
See it in action on jsFiddle: http://jsfiddle/hEvFr/
Sounds like you would want to call JSON.stringify on the values of the key-value pairs.
本文标签: javascriptJQuery JSONwithinJSON parsingStack Overflow
版权声明:本文标题:javascript - JQuery JSON-within-JSON parsing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741735600a2395044.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论