admin管理员组文章数量:1336008
var key = " ";
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email":
{
"email1":"[email protected]",
"email2":"[email protected]"},
};
for (key in myBio){
var y = myBio[key];
console.log(key+" : "+ y);
}
output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email : [object Object]
Required Output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email :
- email1:[email protected]
- email2:[email protected]
I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??
var key = " ";
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email":
{
"email1":"[email protected]",
"email2":"[email protected]"},
};
for (key in myBio){
var y = myBio[key];
console.log(key+" : "+ y);
}
output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email : [object Object]
Required Output:
- name : Sathya
- age : 23
- position : Soft.Engineer
- email :
- email1:[email protected]
- email2:[email protected]
I can print this Emails separately using another loop. But I want to print with main loop only . Any ways to do this using JavaScript??
Share Improve this question edited Jan 11, 2017 at 9:55 HaBo 14.3k39 gold badges118 silver badges214 bronze badges asked Jan 11, 2017 at 9:47 Sathyanarayana K USathyanarayana K U 111 silver badge3 bronze badges 2-
It's because you try to concatentate object to string. You don't have to do that. Solution is to use
console.log(key, y)
– Piotr Lewandowski Commented Jan 11, 2017 at 9:53 - did you try simple console.log(myBio)? – Ayyash Commented Jan 11, 2017 at 10:00
5 Answers
Reset to default 5Most people searching in Google and finding this question are probably looking for something like this:
console.log(JSON.stringify(object, null, 2));
This prints deeply nested JSON to the console without the annoying "[Object]".
The last two parameters are optional. The 2 says to pretty print the JSON with an indent of 2. If left blank, will output non-formatted JSON.
This does not answer this specific question, but hopefully will answer those searching for "Print nested JSON Javascript".
You could use a recursive function to get this done.. example
function logRecursive(object){
for (key in object){
var value=object[key];
if(typeof value === 'object'){
console.log('{');
logRecursive(value)
console.log('}');
}else{
console.log(value);
}
}
function recursion(myBio) {
for (var key in myBio) {
if (typeof(myBio[key]) == 'object') {
recursion(myBio[key]);
} else {
alert("Key: " + key + " Values: " + myBio[key]);
}
}
}
use this subroutine if you have nested json
Try this:
var myBio = {
"name" : "Sathya",
"age" : "23",
"position" : "Soft.Engineer",
"email" : {
"email1" : "[email protected]",
"email2" : "[email protected]"
},
};
function print(bio) {
for (var key in bio) {
if (typeof(bio[key]) == 'object') {
print(bio[key]);
} else {
console.log(key + ": " + bio[key]);
}
}
}
print(myBio);
Try this :
var myBio = {
"name":"Sathya",
"age":"23",
"position":"Soft.Engineer",
"email": {
"email1":"[email protected]",
"email2":"[email protected]"
}
};
var result = parseJSON(myBio);
console.log(result);
function parseJSON(obj) {
var parsedData = '';
for(var i in obj) {
if (typeof obj[i] == 'object') {
parsedData += parseJSON(obj[i]);
}else {
parsedData += i +' : '+ obj[i];
}//end if
parsedData += '\n';
}//end for
return parsedData;
}//end function
本文标签: javascriptTrying to print the Nested JsonStack Overflow
版权声明:本文标题:javascript - Trying to print the Nested Json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741806184a2398517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论