我正在使用包含以下项目的JSon文件:
{
"resource":"A",
"literals":["B","C","D"]
}
我想只检索B,C和D项,并将它们作为字符串存储在数组中。这是我的代码:
<script>
// Reading the JSon file that has the items above
$.getJSON( "/Users/Docs/sample.json", function( data ) {
$.each( data, function( key, val ) {
items.push( "<li id='" + key + "'>" + val + "</li>" );
});
</script>
有没有人可以帮助我只获取B,C和D并将它们放入一个字符串数组中,以便我可以在以后的另一个脚本中重复使用它们?非常感谢您的帮助。
答案 0 :(得分:1)
除非我误解了这个问题,否则你需要通过data.literals
访问对象中的数组。试试这个:
$.getJSON("/Users/Docs/sample.json", function(data) {
$.each(data.literals, function(i, val) {
items.push("<li id='" + val + "'>" + val + "</li>" );
});
});