json_decode到数组 [英] json_decode to array

查看:126
本文介绍了json_decode到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON字符串解码为数组,但是出现以下错误.

I am trying to decode a JSON string into an array but i get the following error.

致命错误:无法使用类型的对象 stdClass作为数组 C:\ wamp \ www \ temp \ asklaila.php在线 6

Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\temp\asklaila.php on line 6

这是代码:

<?php
$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);
print_r($obj['Result']);
?>

推荐答案

根据文档,您需要指定是否要关联数组而不是json_decode中的对象,这将是代码:

As per the documentation, you need to specify if you want an associative array instead of an object from json_decode, this would be the code:

json_decode($jsondata, true);

这篇关于json_decode到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆