【问题标题】:JSON string looping issueJSON字符串循环问题
【发布时间】:2012-06-15 04:37:14
【问题描述】:

我有一个 json 字符串

[{"part_id":"66","part_name":"Palet crate","part_image":"crate-box.gif","0":{"language_data_content":"Length [mm]","pgd_value":"1000"},"1":{"language_data_content":"Width [mm]","pgd_value":"800"},"2":{"language_data_content":"Height [mm]","pgd_value":"800"},"3":{"language_data_content":"Thickness [mm]","pgd_value":"20"}}]

这是我在控制器中的操作的 Ajax 响应的一部分

  for($i=0;$i<count($partlist);$i++){              
            $jsondata[$i]['part_id'] = $partlist[$i]['part_id'];
            $jsondata[$i]['part_name'] = $partlist[$i]['part_name'];
            $jsondata[$i]['part_image'] = $partlist[$i]['part_image'];
            $gdata = $pgdata->getPropertyDimensions($partlist[$i]['part_id'],1);
            if(count($gdata) > 0){
                $j = 0;
                foreach($gdata as $g){
                    $jsondata[$i][$j]['language_data_content'] = $g->language_data_content;
                    $jsondata[$i][$j]['pgd_value'] = $g->pgd_value;
                    $j++;
                }
            }           
        } 
       echo json_encode($jsondata);exit;

对于一个部分 id 可以有多个 pgd_value 在这个 json 数组中只有一个部分 id 和四个 pgd_value..on 我的 ajax 成功函数循环这个 json作为

success:function(msg){
str = '';
  if(msg.length > 0){
                for(j=0;j<msg.length;j++){
   str +='<span>'+msg[j]['part_id']+'</span>';
   // here i want to loop those four **pgd_value** values from json is it possible ?

}
}

}

【问题讨论】:

  • 这段代码有什么问题?

标签: jquery ajax arrays json


【解决方案1】:
var msg = [{
    "part_id": "66",
    "part_name": "Palet crate",
    "part_image": "crate-box.gif",
    "0": {
        "language_data_content": "Length [mm]",
        "pgd_value": "1000"
    },
    "1": {
        "language_data_content": "Width [mm]",
        "pgd_value": "800"
    },
    "2": {
        "language_data_content": "Height [mm]",
        "pgd_value": "800"
    },
    "3": {
        "language_data_content": "Thickness [mm]",
        "pgd_value": "20"
    }}];
    var data = msg[0]; // extract the whole object from array
    // loop over object
    for (var key in data) {
        // checking for key which contain pgd_value
        // as you mentioned about only 4 pgd_values 
        // so here is the checking for just 4 index
        if (key == '0' || key == '1' || key == '2' || key == '3') {
            // here I used square bracket notation to 
            // retrieve data from object
            alert(data[key].pgd_value);
        }
    }

DEMO

【讨论】:

    【解决方案2】:

    您可以使用jQuery.map 选择pgd_values。看到这个小提琴:http://jsfiddle.net/HackedByChinese/6LLVe/1/

    var str = '';
    if (msg.length > 0) {
        for (j = 0; j < msg.length; j++) {
            var pgdValues = $.map(msg[j], function(item) {
                if (item['pgd_value']) return item['pgd_value'];
            });
            str += '<span> Part ID ' + msg[j]['part_id'] + ' pgd_values ' + pgdValues.join(', ') + '</span>';
            // OP: here i want to loop those four **pgd_value** values from json is it possible ?
            // ME: yep. all the IDs are now accessible from `pgdValues`
    
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      您确实应该考虑将 JSON 反序列化为实际对象。它更容易使用,它使您的代码更具可读性。

      请看这里:Deserializing from JSON into PHP, with casting?

      【讨论】:

        【解决方案4】:

        这应该可行。

        $.each(msg,function(index,item){
        
            alert("Part_ID : "+item.part_id);
            alert(item[0].pgd_value)
            alert(item[1].pgd_value)
            alert(item[2].pgd_value)
            alert(item[3].pgd_value)
        
        })
        

        示例http://jsfiddle.net/Mc2du/29/

        但如果可能,请尝试更改您的 JSON 以发送一些字符串表达式而不是 0/1。所以阅读它既容易又健壮)。读取索引是不安全的,因为向 JSON 结构添加新内容会破坏读取代码。

        假设您将 0/1 方法替换为这样的字符串表达式(将 0 替换为 Item0)。你可以这样读

         $.each(msg,function(index,item){
            alert("Part_ID : "+item.part_id);
            alert(item.Item1.pgd_value)
            alert(item.Item2.pgd_value)
            alert(item.Item3.pgd_value)
         })
        

        示例:http://jsfiddle.net/Mc2du/28/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-01
          • 2015-01-12
          • 2012-01-14
          • 1970-01-01
          • 2021-05-07
          • 1970-01-01
          • 1970-01-01
          • 2019-01-03
          相关资源
          最近更新 更多