javascript - 将 JSON 键/数组值的多个实例访问到 HTML
问题描述
我的第一个/实例输出很好。但是,如果从“ ZipSearch
”变量中找到多个(这是 JSON 标头,即在下面的 JSON 代码中,父23222
值), "23222":[ ...
下面是最近尝试使用[2]
也尝试过[1]
等。关于如何输出以下 JSON 数据的内容与公共 Key 值的任何想法?进入我的 HTML 页面?
$('span.zip').html(myjson[ZipSearch][0].Zipcode);
$('span.state').html(myjson[ZipSearch][0]["Primary State"]);
$('span.city').html(myjson[ZipSearch][0].City);
$('span.countyS').html(myjson[ZipSearch][0]["County S"]);
$('span.county').html(myjson[ZipSearch][0].County);
$('span.zip1').html(myjson[ZipSearch][2].Zipcode);
$('span.state1').html(myjson[ZipSearch][2]["Primary State"]);
$('span.city1').html(myjson[ZipSearch][2].City);
$('span.countyS1').html(myjson[ZipSearch][2]["County S"]);
$('span.county1').html(myjson[ZipSearch][2].County);
这是我的JSON。
"23222":[ # sometimes has multiple responses per, ie below
{"Zipcode":"23222","City":"","Primary State":"Virginia","County S":"555","County":"Sample City"},
{"Zipcode":"23222","City":"","Primary State":"Utah","County S":"444","County":"Sample Bigger City"}
],
基本上,我无法在我的HTMLUtah
页面中输出上面的第二行等……只是总是重复第一行的内容。
这是标记:(如果有多个公共数组,我只是想处理内容)
<li class="zip">Zip Code: <span class="zip"></span></li>
<li class="state">Primary State: <span class="state"></span></li>
<li class="city">City: <span class="city"></span></li>
<li class="countySSA">County SSA: <span class="countyS"></span></li>
<li class="county">County: <span class="county"></span></li>
<li class="zip1">Zip Code: <span class="zip1"></span></li>
<li class="state1">Primary State: <span class="state1"></span></li>
<li class="city1">City: <span class="city1"></span></li>
<li class="countySSA1">County SSA: <span class="countyS1"></span></li>
<li class="county1">County: <span class="county1"></span></li>
更新:
所以,我刚刚尝试了这个,在每个答案下面 - 它仍然只是将一组数组值打印到 HTML。
myjson[ZipSearch].forEach(function(innerobj,index){
$('span.zip'+index).html(innerobj["Zipcode"]);
$('span.county'+index).html(innerobj["County"]);
$('span.county1'+index).html(innerobj["County"]);
})
解决方案
如果你的对象是
var obj=[
{"Zipcode":"23222","City":"","Primary State":"Virginia","County S":"555","County":"Sample City"},
{"Zipcode":"23222","City":"","Primary State":"Utah","County S":"444","County":"Sample Bigger City"}
]
用于每个访问多个对象
obj.forEach(function(innerobj,index){
$('span.zip'+index).html(innerobj["Zipcode"]);
})
推荐阅读
- mysql - 从另一个表中检索数据并将其插入到新表中
- r - R 闪亮的应用程序 DataTable 具有类似 excel 的过滤功能,可包含所有内容,但不包含某些元素或相反的内容
- postgresql - Postgres:如何在不影响引用它的其他 MV 的情况下更改 MV 的名称?
- node.js - 尝试在 Angular UI 中显示时,获取的已排序 API 数据(NodeJs &Mongoose)未按排序顺序显示
- javascript - 显示更新的表格而不刷新或重新加载页面
- javascript - 如何将日期值转换为天数
- csv - 使用 Simple Data Writer 将响应数据与其他标签一起保存
- javascript - 使用按钮将每个 td 单元格文本复制到剪贴板
- php - 你如何在 PHP 中发布 JSON 数据?(将原始 cURL 代码转换为 PHP)
- aws-api-gateway - 未从 API Gateway URL 获取标头