从JSON对象获取数据

时间:2018-07-26 05:09:06

标签: php json

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},\
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},\
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},\
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},\
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

echo $response['items'][1]['food'];
  

错误:“字符串偏移量'items'非法

为什么我不能访问数据?

5 个答案:

答案 0 :(得分:4)

在将json字符串用作对象/数组之前,您必须先对其进行解码。
您的问题表明您要将其用作数组,这意味着json_decode的第二个参数应为true。
但是\会使您的字符串无效。是复制粘贴错误还是实际的字符串?
如果它是实际字符串,则可能需要在解码之前删除\
我认为这是复制粘贴错误?

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$arr = json_decode($response, true);
echo $arr['items'][1]['food']; // beans

https://3v4l.org/RiqSo


如果您需要删除不需要的\,则可以使用反斜杠。

$arr = json_decode(stripslashes($response), true);

https://3v4l.org/WslYh


要复制该错误,您需要执行以下操作:

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},\
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},\
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},\
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},\
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$arr = (string)json_decode($response, true); // json_decode returns NULL and the string cast makes it "NULL"
echo $arr['items'][1]['food']; // Warning: Illegal string offset 'items'
// or
echo $response['items'][1]['food']; // Warning: Illegal string offset 'items'

答案 1 :(得分:2)

尝试一下

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$response_json = json_decode($response);
echo $response_json->items[2]->food.' | '.$response_json->items[2]->condition;

output : yam | not paid

答案 2 :(得分:1)

使用json_decode将字符串解码为json。

http://php.net/manual/en/function.json-decode.php请参考此链接以获取更多信息

答案 3 :(得分:1)

您访问的数据为json格式。因此,首先您必须使用json_decode();

将其转换为普通数组

这是代码。

$response = json_decode($response, true);

现在是数组。因此可以像这样访问其数据。

echo $response['items'][1]['food']; 

输出:

beans

答案 4 :(得分:1)

您需要使用json_decode()函数将第一个json转换为数组。

$response = '{"items":
     [{"id":"1","food":"rice","Amount":"100","condition":"paid"},
      {"id":"2","food":"beans","Amount":"200","condition":"paid"},
      {"id":"3","food":"yam","Amount":"50","condition":"not paid"},
      {"id":"4","food":"tomatoes","Amount":"100","condition":"paid"},
      {"id":"5","food":"potato","Amount":"700","condition":"paid"}]}';

$getarray = json_decode($response);

echo $getarray->items[0]->food;