-2

我正在开发一个以 json 格式提供输出的情感 api,现在我想用 php 解析它

{ "status": "OK", "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html", "url": "", "language": "english", "docSentiment": { "type": "neutral" } }

这是我的输出 json 格式。

我只想输出"type" = "neutral"

4

4 回答 4

2

您可以使用json_decode()将其解码为 PHP 对象或数组。

下面我添加了一个使用 2 中的任何一个的示例。

$json = '{
    "status": "OK",
    "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
    "url": "",
    "language": "english",
    "docSentiment": {
        "type": "neutral"
    }
}';

// Convert to associative array  
// (remove the second parameter to make it an object instead)
$array = json_decode($json, true);
$object = json_decode($json);

// Output the docSentiment type
echo $array['docSentiment']['type']; // Output: 'neutral'
echo $object->docSentiment->type; // Output: 'neutral'
于 2013-08-20T08:49:09.730 回答
1

您可以使用json_decode然后转到相关的事情:

 $json=<<<JSON
{ "status": "OK", "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html", "url": "", "language": "english", "docSentiment": { "type": "neutral" } }
JSON;
$json = json_decode($json);
echo $json->docSentiment->type;

输出:neutral

于 2013-08-20T08:48:24.787 回答
0

尝试阅读有关如何解码 json 的手册。

这将为您提供数据的 PHP 版本:

$strJson = '{ "docSentiment": { "type": "neutral" } }';
$data = json_decode($strJson);
var_dump($data);

这个答案很容易在谷歌上找到......

于 2013-08-20T08:47:35.217 回答
0

使用json_decode , 这是 PHP 版本的数据解码

于 2013-08-20T08:48:26.770 回答