【问题标题】:PHP Azure OCR - convert JSON in ArrayPHP Azure OCR - 在数组中转换 JSON
【发布时间】:2018-02-15 03:07:44
【问题描述】:

我正在使用 Azure OCR 服务,现在我想将获得的数据保存在一个数组中。 不幸的是,我总是收到此错误消息:

PHP Warning:  json_decode() expects parameter 1 to be string, object given in XXX on line 43

我的输出列表:

{"language":"en","textAngle":-2.5000000000000346,"orientation":"Up","regions":[{"boundingBox":"896,2104,1528,688","lines":[{"boundingBox":"896,2104,1528,688","words":[{"boundingBox":"896,2104,1528,688","text":"DFG"}]}]}]}

我的代码是:

    $request->setHeader($headers);
    $parameters = array(
        // Request parameters
        'language' => 'unk',
        'detectOrientation ' => 'true',
    );

    $url->setQueryVariables($parameters);
    $request->setMethod(HTTP_Request2::METHOD_POST);   
    $body = array('url' => 'https://i.imgur.com/2sUbQC8.jpg');
    $request->setBody(json_encode($body));

    // Request body


    try
    {
        $response = $request->send();
        echo $response->getBody();
        $json = json_decode($response, true);

    }
    catch (HttpException $ex)
    {
        echo "Fehler :(";
        echo $ex;
    }

?>

【问题讨论】:

  • 试试$json = json_decode($response->getBody(), true)

标签: php arrays json azure ocr


【解决方案1】:

你不应该打电话给echo $response->getBody(); 相反,将其分配给一个变量:

$json = $response->getBody();

然后你可以把它变成一个数组:

$array = json_decode($json, true);

【讨论】:

    猜你喜欢
    • 2018-09-16
    • 2021-08-05
    • 2022-11-04
    • 2016-08-01
    • 2015-07-02
    • 2016-07-07
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多