解析php文件中的json格式输出

时间:2013-08-20 08:44:19

标签: php json

我正在研究一种以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 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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

答案 2 :(得分:0)

尝试reading the manual如何解码json。

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

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

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

答案 3 :(得分:0)

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