【问题标题】:Accessing specific JSON value访问特定的 JSON 值
【发布时间】:2011-10-05 10:33:15
【问题描述】:

我目前有一个变量 $return ,其中包含 JSON 编码的数据,输出为:

({0:{data:[[0, null], [1, null], [2, null], [3, null], [4, null], [5, null], [6, null], [7, null], [8, null], [9, null], [10, null], [11, null], [12, null], [13, null], [14, null], [15, null], [16, null], [17, null], [18, null], [19, null], [20, null], [21, null], [22, null], [23, null]], label:null, count:null}, 

等等(复制和粘贴太多)。基本上我想要做的是找出 data:[0,null] 的值是否为空,然后根据结果创建一个 if else 语句。如果它为空,我需要显示一条消息“无可用数据”,但如果它包含一个值,我需要显示该值。

有人可以解释一下我将如何访问该特定值吗?

感谢您的帮助

【问题讨论】:

    标签: php javascript jquery json


    【解决方案1】:

    您是否尝试过使用 http://www.php.net/json_decode 将 JSON 转换为数组,然后像访问普通数组一样访问元素?

    【讨论】:

      【解决方案2】:
      $return=json_decode($return);
      if($return['data'][0]){
      // what to do if null
      }else{
      // what to do if not (alternatively use elseif())
      }
      

      【讨论】:

        【解决方案3】:

        有一个名为 json_decode() 的函数。 它只会返回原始的 json 字符串数组。

        $ary = array(); 
        $ary = json_decode($json_string);
        echo "<pre>";print_r($ary);echo "</pre>";
        

        【讨论】:

          猜你喜欢
          • 2018-01-02
          • 2016-08-09
          • 2022-11-16
          • 2018-09-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-28
          相关资源
          最近更新 更多