不能使用stdClass类型的对象作为数组?

我使用了一个奇怪的错误json_decode()。它正确解码数据(我看到它使用print_r),但当我尝试访问数组内的信息时,我得到:


Fatal error: Cannot use object of type stdClass as array in

C:\Users\Dail\software\abs.php on line 108

我只是想做:返回的数据$result['context']在哪里$resultjson_decode()


如何读取此数组中的值?


沧海一幻觉
浏览 591回答 3
3回答

潇湘沐

使用第二个参数json_decode使其返回一个数组:$result = json_decode($data, true);

慕无忌1623718

该函数json_decode()默认返回一个对象。您可以像这样访问数据:var_dump($result->context);如果你有像这样from-date的标识符(使用上述方法时连字符会导致PHP错误)你必须写:var_dump($result->{'from-date'});如果你想要一个数组,你可以这样做:$result = json_decode($json, true);或者将对象强制转换为数组:$result = (array) json_decode($json);

凤凰求蛊

您必须使用它来访问它,->因为它是一个对象。更改您的代码:$result['context'];至:$result->context;
打开App,查看更多内容
随时随地看视频慕课网APP