admin管理员组文章数量:1394183
I have a json file with content in this format:
{
"Florida":{
"4079823456" : "Text message content 1 here",
"4079323457" : "Text message content 2 here",
"4079823458" : "Text message content 3 here"
},
"Texas":{
"2149823456" : "Text message content 1 here",
"2149323457" : "Text message content 2 here",
"2149823458" : "Text message content 3 here"
}
}
I am using javascript (no libraries) to output the key/value pairs in the json file to an html table to get an output like this:
Phone Number | Text Message
4079823456 | Text message content 1 here
4079323457 | Text message content 2 here
But instead, I get something like this:
Phone Number | Text Message
0 {
1 "
The following is a code snippet from my javascript:
var str = JSON.stringify(http_request.responseText);
var obj = JSON.parse(str);
var rowContent;
var tablerows = document.getElementById("tablerow");
for(var key in obj){
rowContent = "<tr><td>" + key + "</td><td>" + obj[key] + "</td></tr>";
tablerows.innerHTML += rowContent;
}
How can I get the key value pairs to output to the desired output instead of getting each individual character in my JSON file? Ideally, I would like to loop through both the key and the value (in my case, the phone numbers and text messages) as there are actually hundreds of them.
Thanks in advance.
I have a json file with content in this format:
{
"Florida":{
"4079823456" : "Text message content 1 here",
"4079323457" : "Text message content 2 here",
"4079823458" : "Text message content 3 here"
},
"Texas":{
"2149823456" : "Text message content 1 here",
"2149323457" : "Text message content 2 here",
"2149823458" : "Text message content 3 here"
}
}
I am using javascript (no libraries) to output the key/value pairs in the json file to an html table to get an output like this:
Phone Number | Text Message
4079823456 | Text message content 1 here
4079323457 | Text message content 2 here
But instead, I get something like this:
Phone Number | Text Message
0 {
1 "
The following is a code snippet from my javascript:
var str = JSON.stringify(http_request.responseText);
var obj = JSON.parse(str);
var rowContent;
var tablerows = document.getElementById("tablerow");
for(var key in obj){
rowContent = "<tr><td>" + key + "</td><td>" + obj[key] + "</td></tr>";
tablerows.innerHTML += rowContent;
}
How can I get the key value pairs to output to the desired output instead of getting each individual character in my JSON file? Ideally, I would like to loop through both the key and the value (in my case, the phone numbers and text messages) as there are actually hundreds of them.
Thanks in advance.
Share Improve this question asked Jul 7, 2016 at 21:33 user2554121user2554121 2251 gold badge8 silver badges18 bronze badges 1- 1 The response text was already "stringified". Just parse the response text instead of str and your code should work. – Travis J Commented Jul 7, 2016 at 21:36
2 Answers
Reset to default 2For your data you can use two for...in
loops and add to table
. Also its better to create string with table content and add it after loop is finished.
var data = {
"Florida": {
"4079823456": "Text message content 1 here",
"4079323457": "Text message content 2 here",
"4079823458": "Text message content 3 here"
},
"Texas": {
"2149823456": "Text message content 1 here",
"2149323457": "Text message content 2 here",
"2149823458": "Text message content 3 here"
}
}
var table = document.querySelector('table');
var rows = '';
for (var p in data) {
for (var k in data[p]) {
rows += '<tr><td>' + k + '</td><td>' + data[p][k] + '</td></tr>'
}
}
table.innerHTML = rows;
<table></table>
You don't need to call JSON.stringify()
, your response is already a JSON string.
Instead, do the following:
var obj = JSON.parse(http_request.responseText);
本文标签: Output JSON keyvalue in JavaScriptStack Overflow
版权声明:本文标题:Output JSON key, value in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744722910a2621802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论