【问题标题】:How to decode a JSON String如何解码 JSON 字符串
【发布时间】:2011-02-02 08:34:52
【问题描述】:

大家!我可以请你帮我解码这个 JSON 代码吗:

$json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';

我想把上面的结构组织成这样:

注1:

文件夹:收件箱

来自(从):...

日期(日期):...

时间(时间):...

utcOffsetSeconds: ...

收件人(地址):...

收件人(姓名):...

状态(deliveryStatus):...

文本(正文):...

注2:

...

提前致谢!

【问题讨论】:

    标签: php json


    【解决方案1】:

    您可以使用json_decode 函数来解码您的 JSON 字符串:

    $json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';
    $data = json_decode($json);
    var_dump($data);
    


    你会得到这样的东西:

    object(stdClass)[1]
      public 'inbox' => 
        array
          0 => 
            object(stdClass)[2]
              public 'from' => string '55512351' (length=8)
              public 'date' => string '29/03/2010' (length=10)
              public 'time' => string '21:24:10' (length=8)
              public 'utcOffsetSeconds' => int 3600
              public 'recipients' => 
                array
                  0 => 
                    object(stdClass)[3]
                      public 'address' => string '55512351' (length=8)
                      public 'name' => string '55512351' (length=8)
                      public 'deliveryStatus' => string 'notRequested' (length=12)
              public 'body' => string 'This is message text.' (length=21)
          1 => 
            object(stdClass)[4]
              public 'from' => string '55512351' (length=8)
              public 'date' => string '29/03/2010' (length=10)
              public 'time' => string '21:24:12' (length=8)
              public 'utcOffsetSeconds' => int 3600
              public 'recipients' => 
                array
                  0 => 
                    object(stdClass)[5]
                      public 'address' => string '55512351' (length=8)
                      public 'name' => string '55512351' (length=8)
                      public 'deliveryStatus' => string 'notRequested' (length=12)
              public 'body' => string 'This is message text.' (length=21)
          ....
          ....
    


    现在您知道了数据的结构,您可以对其进行迭代;例如,你可以使用这样的东西:

    foreach ($data->inbox as $note) {
      echo '<p>';
      echo 'From : ' . htmlspecialchars($note->from) . '<br />';
      echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
      echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
      echo '</p>';
    }
    


    你会得到这样的输出:

    From : 55512351
    Date : 29/03/2010
    Body : This is message text.
    
    From : 55512351
    Date : 29/03/2010
    Body : This is message text.
    
    ...
    ...
    

    【讨论】:

    • Pascal MARTIN,感谢您的快速反应!但是这个乘法数组的问题。我真的不能对所有这些数组进行操作并制作一个简单的结构,就像我想在上面做的那样,就像单独的注释一样!拜托,我问你能不能展示我应该如何操作所有这些数组?你能用我不知道 foreach() 或什么来展示它吗?谢谢!
    • @ilnur777:如果您不知道foreach,请阅读文档:php.net/manual/en/control-structures.foreach.phpforforeach 循环是处理数组时必不可少的工具。
    • 帕斯卡·马丁,你救了我的命和神经!!!非常感谢。我现在真的可以理解如何在 JSON 上操作数组了!
    • @ilnur777:不要忘记将 Pascal 的答案标记为正确答案 :-)
    • 如果只有一个收件人,$note-&gt;recipients[0]-&gt;name 之类的应该可以;如果有时不止一个,您可以使用 foreach 循环遍历 $note-&gt;recipients
    【解决方案2】:

    看起来收件人属性是一个数组,试试这个:

    $json = '{"inbox":[{"from":"55512351","date":"29\/03\/2010","time":"21:24:10","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:12","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."},{"from":"55512351","date":"29\/03\/2010","time":"21:24:13","utcOffsetSeconds":3600,"recipients":[{"address":"55512351","name":"55512351","deliveryStatus":"notRequested"}],"body":"This is message text."}]}';
    $data = json_decode($json);
    print_r($data);
    
        foreach ($data->inbox as $note)
        {
          echo '<p>';
          echo 'From : ' . htmlspecialchars($note->from) . '<br />';
          echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
          echo 'Time : ' . htmlspecialchars($note->time) . '<br />';
          echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
    
            foreach($note->recipients as $recipient)
            {
                echo 'To (address) : ' . htmlspecialchars($recipient->address) . '<br />';
                echo 'To (name)    : ' . htmlspecialchars($recipient->name) . '<br />';
                echo 'Status       : ' . htmlspecialchars($recipient->deliveryStatus) . '<br />';
            }
        }
    

    【讨论】:

      【解决方案3】:
      enter code here
      <?php
      $subject = file_get_contents('http://example.com/subject.php');
      $subject_arr = json_decode($subject);
      
      $my = $subject_arr->info;
      
      for($i=0;$i<=1000;$i++){
      echo $my[$i]->subject_name;
      echo "<br>";
      }
      echo $my[0]->subject_name;
      
      ?>
      <?php
      $subject = file_get_contents('http://example.com/subject.php');
      $subject_arr = json_decode($subject);
      
      $my = $subject_arr->info;
      
      for($i=0;$i<=1000;$i++){
      echo $my[$i]->subject_name;
      echo "<br>";
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 2011-08-27
        • 2011-01-15
        • 2019-04-03
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多