从URL / API获取数据(作为字符串)之后,如何在PHP中将字符串类型数据转换为对象类型数组

时间:2017-07-05 07:03:36

标签: php json

来自'file_get_contents(url)'的数据是

$str = // example google.visualization.Query.setResponse(
{"x":"0.6","y":{"a":[{"id":"xyz","label":"xyz"},{"id":"xyz","label":"xyz"}],"b":[{"c":[{"id":"xyz","label":"xyz"},{"id":"xyz","label":"xyz"}]}]}});

我想将此字符串转换为对象类型数组,我已经尝试过' json_decode($ str,true)'和 html_entity_decode($ str),但确实如此成功我使用了 json_last_error()也返回4 我想要这样的输出:

    // example google.visualization.Query.setResponse(
{
    "x":"0.6",
    "y":
        {
            "a":[
                {"id":"xyz","label":"xyz"},
                {"id":"xyz","label":"xyz"}
            ],
            "b":[
                    {"c":[
                        {"id":"xyz","label":"xyz"},
                        {"id":"xyz","label":"xyz"}
                    ]}
                ]
        }
});

之后我正在使用

$sValidJson = str_replace("// Data table response google.visualization.Query.setResponse(","",$str);
$sValidJson = str_replace(");","",$sValidJson);
$myArray = json_decode(json_encode($sValidJson), true);

现在它不会为json生成错误,但仍然将其类型返回为字符串而不是json对象

0 个答案:

没有答案
相关问题