简体   繁体   English

为foreach()提供的JSON无效参数

[英]JSON Invalid argument supplied for foreach()

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 : 我有一些问题要从JSON检索数据,它总是说为foreach()提供了Invalid参数 ,这是url.php中的JSON:

[{"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"}]

and this is my code : 这是我的代码:

<?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/>";
}
?>

I've been trying different things to retrieve the json but still not having the result, any idea for this ? 我一直在尝试不同的方法来检索json,但仍然没有结果,对此有任何想法吗?

thanks in advance 提前致谢

You need to explicitly turn json_decode output to an array. 您需要显式将json_decode输出转换为数组。 Like this: 像这样:

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

I had exactly same issue like yours and this resolved my issue. 我遇到了与您完全相同的问题,这解决了我的问题。

Just remove the true in the json_decode() like this: 只需像这样删除json_decode()中的true json_decode()

$koyim=  json_decode($json);

Output: 输出:

0 IT SERVICE & SOLUTION
0 SUBDIV BUSINESS SERVICE
100.1446 Data Analytics

Because if you look into the manual: http://php.net/manual/en/function.json-decode.php 因为如果您查看手册: http : //php.net/manual/zh/function.json-decode.php

And take a quote from there: 并从那里引用:

assoc When TRUE, returned objects will be converted into associative arrays. assoc为TRUE时,返回的对象将转换为关联数组。

Please take a look at the assoc flag that you are setting to true when you call json_decode : http://php.net/manual/en/function.json-decode.php 请查看当您调用json_decode时设置为true的assoc标志: http : json_decode

assoc
When TRUE, returned objects will be converted into associative arrays.

When assoc flag is set to true the returned objects will be converted into associative arrays, with that being said, the way you can access your properties will be using [] not -> 当assoc标志设置为true ,返回的对象将被转换为关联数组,也就是说,访问属性的方式将使用[]而不是->

it should work fine using the below code: 使用以下代码应该可以正常工作:

<?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/>";
}
?>

Since your JSON is valid, next step is to check for PHP error in decoding. 由于您的JSON有效,因此下一步是检查解码中的PHP错误。

First try this code to check error while decoding JSON: 首先尝试使用以下代码在解码JSON时检查错误:

<?php
    $url="url.php";
    $json = file_get_contents($url);
    $koyim=  json_decode($json,true);

    // Add this switch to your code
    switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}
// end of switch

foreach($koyim as $data){
echo $data['nilai'];
echo $data['nama'];
echo "<br/>";
}
?>

Once you know the error you can search for more precise error from CONSTANT in case. 一旦知道了错误,便可以从CONSTANT中搜索更精确的错误,以防万一。 Most like its 'JSON_ERROR_UTF8'. 最喜欢它的“ JSON_ERROR_UTF8”。

For this you can try this code: $url = "url.php"; 为此,您可以尝试以下代码:$ url =“ url.php”;

$json = file_get_contents($url);
$json = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($json));

$koyim = json_decode($json,true);

i was have this error too, and i have solved it using this: 我也有这个错误,我已经用这个解决了:

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

so, your code should be like this: 因此,您的代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM