如何阅读这个JSON?

时间:2011-02-25 08:49:09

标签: javascript jquery json

对不起,如果这是一个新手问题,但我对如何阅读这个json字符串感到困惑:

{
    "VF001JV018":[
      {
         "kode_question":"VF001JV018",
         "kode_option":1,
         "kode_option_tipe":"PF006",
         "nama_option":"Pilihan 1",
         "reference_db":null,
         "reference_table":null,
         "reference_field":null,
         "required_value":0,
         "reference_pk":null,
         "status":1
      },
      {
         "kode_question":"VF001JV018",
         "kode_option":4,
         "kode_option_tipe":"PF006",
         "nama_option":"Pilihan 4",
         "reference_db":null,
         "reference_table":null,
         "reference_field":null,
         "required_value":0,
         "reference_pk":null,
         "status":1
      },
      {
         "kode_question":"VF001JV018",
         "kode_option":5,
         "kode_option_tipe":"PF006",
         "nama_option":"Lainnya",
         "reference_db":null,
         "reference_table":null,
         "reference_field":null,
         "required_value":1,
         "reference_pk":null,
         "status":1
      }
   ],
   "VF001JV020":[
      {
         "kode_question":"VF001JV020",
         "kode_option":1,
         "kode_option_tipe":"PF001",
         "nama_option":"Kode Toko",
         "reference_db":"crm",
         "reference_table":"customer",
         "reference_field":"kode_customer",
         "required_value":0,
         "reference_pk":"          ",
         "status":1
      }
   ]
}

如何在列表中显示数据?例如:

<ul>
<li><strong>VF001JV018</strong></li>
<li>VF001JV018-1</li>
<li>VF001JV018-4</li>
<li>VF001JV018-5</li>
<li><strong>VF001JV020</strong></li>
<li>VF001JV020-1</li>
</ul>

我使用jquery每个函数循环每个元素但仍然没有运气

$.getJSON('data.json', null, function(response){
    var echo = '<ul>';
    $(response).each(function(k,v){
        echo += '<li><strong>'+k+'</strong> : '+v+'</li>';
    });
    echo += '</ul>';
    $('#here').html(echo);
});

1 个答案:

答案 0 :(得分:2)

response不是HTML字符串或DOM元素。你不能以这种方式将它传递给jQuery。

使用jQuery.each循环遍历它:

$.each(response, function(k, v) {
    echo += '<li><strong>'+k+'</strong></li>';
});

您无法将字符串与v连接,因为v又是一个数组。您必须循环它并访问您要打印的特定信息,例如v[i].status