从json php codeigniter获取价值

时间:2013-07-22 07:14:50

标签: codeigniter codeigniter-url

我有像这样的json对象

[
  [
    {
      "class":"com.ehealth.data.Sample_Data",
      "collectedBy":"2013-07-21",
      "collectedDate":"Kamal",
      "orderID":2,
      "sampleID":2.897033553E9
    },
    {
      "class":"com.ehealth.data.Order_Data",
      "doctorUsername":"Kamal",
      "dueDate":"2014-01-02",
      "orderDate":"2013-12-12",
      "orderID":2,
      "patientID":"P0001",
      "prority":1,
      "status":"complete",
      "testType":"Fasting Blood Sugar"
    }
  ],
  [
    {
      "class":"com.ehealth.data.Sample_Data",
      "collectedBy":"2013-07-22",
      "collectedDate":"Kamal",
      "orderID":3,
      "sampleID":5.978956192E9
    },
    {
      "class":"com.ehealth.data.Order_Data",
      "doctorUsername":"Kamal",
      "dueDate":"2014-01-02",
      "orderDate":"2013-12-12",
      "orderID":3,
      "patientID":"P0001",
      "prority":2,
      "status":"complete",
      "testType":"Fasting Blood Sugar"
    }
  ]
]

我将其解码为php数组

$data['query'] = json_decode($curl_response);

我尝试像这样访问valuse

foreach ($query as $row) {
    echo $row->sampleID;           
}

但是当我要访问其中的值时,我无法访问我收到错误

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object 

任何人都可以帮助我。我不知道该怎么做

2 个答案:

答案 0 :(得分:1)

你可以得到它:

foreach ($query as $row) {
   echo $row[0]->sampleID;           
}

答案 1 :(得分:0)

<?php
$str = '[[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-21","collectedDate":"Kamal","orderID":2,"sampleID":2.897033553E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":2,"patientID":"P0001","prority":1,"status":"complete","testType":"Fasting Blood Sugar"}],[{"class":"com.ehealth.data.Sample_Data","collectedBy":"2013-07-22","collectedDate":"Kamal","orderID":3,"sampleID":5.978956192E9},{"class":"com.ehealth.data.Order_Data","doctorUsername":"Kamal","dueDate":"2014-01-02","orderDate":"2013-12-12","orderID":3,"patientID":"P0001","prority":2,"status":"complete","testType":"Fasting Blood Sugar"}]]';

$json = json_decode($str,true);

foreach ($json as $datas){
    foreach ($datas as $data){
        echo (isset($data['sampleID']) ? $data['sampleID'] :'')."<br/>";
    }
}

echo '<pre>';
print_r ($json);
echo '</pre>';
// exit;

?>