【问题标题】:Json to php array (json_decode()) not working [duplicate]Json到php数组(json_decode())不起作用[重复]
【发布时间】:2018-10-27 15:31:56
【问题描述】:

我进行 API 调用并尝试将 json 响应转换为 php 数组。然而,当检查 is_array 函数时,发现它不是一个数组。

调用 API

$ch = curl_init("https://api.url.com/value/value");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , "token"));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);

Json API 调用返回:

[
  {
    "number":"65",
    "Field":"test",
    "Name":"test",
    "type":"Numeric",
    "MaximumLength":128,
    "MinimumLength":0,
    "Options":"required"
  }
]

等等。

我使用解码它

json_decode($result);

但是,像这样检查

if (is_array($result)) {
  echo "is array";
} else { 
  echo "is not an array!";
}

回显“不是数组”。

我检查了 json 响应,它是有效的 json 代码。 我也试过了

json_decode($result, true);

结果相同。

我是否犯了一些明显的错误?

【问题讨论】:

  • json 响应好像不完整
  • @ka_lin 我编辑了这个问题,所以它是正确的。我把它缩短了,以免用 json 聚集整个问题。 Jsonlint 在检查 json 是否正确时返回“有效”。
  • 尝试使用is_object 而非is_array 进行验证
  • @MouradKaroudi 刚试过,结果一样。
  • $result = json_decode($result); 你是在解码后重新分配新元素吗?它工作正常。 http://sandbox.onlinephpfunctions.com/code/149cc2763575f18bac9dc669afc11ea3cc26b8d3

标签: php json


【解决方案1】:

以下代码 sn-p 的行为似乎符合预期(回显 1),因此您的 JSON 有效并且可以正常工作。

$result = '[{"ConditionCode":"1","Field":"test","Name":"test","FieldType":"Numeric","MaximumLength":128,"MinimumLength":0,"Options":"required"}]';

$x = json_decode($result, true);

echo($x[0]["ConditionCode"]);

我猜你只是在 $result 上运行了 json_decode? json_decode 不会设置您将其提供给 json 解码数组的变量的值。它只是返回数组,因此您必须将此值分配给另一个变量(或在这种情况下为自身)

试试

$result = json_decode($result, true);

而不是,

json_decode($result, true);

【讨论】:

  • $result 在传递给解码函数之前是一个字符串吗?
  • 我确实将其分配回去。我都试过了, $result = json_decode($result, true);和 $result = json_decode($result); (没有 (,true)。但我得到了相同的结果。
  • 回显 $result 是否完全符合您的预期?
  • 尝试回显 gettype ( $result ) 来检查它的类型。在运行解码之前和之后在 $result 上试一试。
  • 回显$result 返回1,而gettype 返回boolean
【解决方案2】:

您可以使用json_last_error()json_last_error_msg() 来查看您尝试解析的json 有什么问题。我通常使用GuzzleHttp,它带有以下 json_decode 包装器,当解码失败时会抛出异常:

/**
 * Wrapper for json_decode that throws when an error occurs.
 *
 * @param string $json    JSON data to parse
 * @param bool $assoc     When true, returned objects will be converted
 *                        into associative arrays.
 * @param int    $depth   User specified recursion depth.
 * @param int    $options Bitmask of JSON decode options.
 *
 * @return mixed
 * @throws \InvalidArgumentException if the JSON cannot be decoded.
 * @link http://www.php.net/manual/en/function.json-decode.php
 */
function json_decode($json, $assoc = false, $depth = 512, $options = 0)
{
    $data = \json_decode($json, $assoc, $depth, $options);
    if (JSON_ERROR_NONE !== json_last_error()) {
        throw new \InvalidArgumentException(
            'json_decode error: ' . json_last_error_msg());
    }

    return $data;
}

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    相关资源
    最近更新 更多