为foreach()提供的JSON无效参数 [英] JSON Invalid argument supplied for foreach()

查看:77
本文介绍了为foreach()提供的JSON无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从JSON检索数据时遇到问题,它总是说为foreach()提供的参数无效,这是url.php中的JSON:

I have some issue to retrieve data from JSON, it always said Invalid argument supplied for foreach(), this is the JSON in the url.php :

[{"nama":"IT SERVICE & SOLUTION","nilai":0,"periode":"11","tahun":"2014"},{"nama":"SUBDIV BUSINESS SERVICE","nilai":0,"periode":"11","tahun":"2014"},{"nama":"Data Analytics","nilai":100.1446,"periode":"11","tahun":"2014"}]

是我的代码:

<?php
$url="url.php";
$json = file_get_contents($url);
$koyim=  json_decode($json,true);
foreach($koyim as $data){
    echo $data->nilai;
    echo $data->nama;
    echo "<br/>";
}
?>

我一直在尝试不同的方法来检索json,但仍然没有结果,任何想法这吗?

I've been trying different things to retrieve the json but still not having the result, any idea for this ?

预先感谢

推荐答案

您需要明确地转向 json_decode 输出到数组。像这样:

You need to explicitly turn json_decode output to an array. Like this:

$koyim = (array)json_decode($json);

我遇到了与您完全一样的问题,这解决了我的问题。

I had exactly same issue like yours and this resolved my issue.

这篇关于为foreach()提供的JSON无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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