2013-08-20 87 views
-2

我工作的情绪API,它使输出JSON格式 现在我想解析它在PHP解析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格式。

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

+1

的例子只是使用'json_decode'! '$ decode = json_decode($ jsonString,true); echo $ decode ['docSentiment'] ['type'];'[check here](http://codepad.org/UnbsM1HJ) –

+0

Json格式需要':'不是'=' – Bora

+1

这个问题已被多次询问并且在互联网上很容易找到。你显然没有做任何事情找到一个解决方案之前发布它在这里... – NDM

回答

0

尝试如何JSON解码reading the manual

这会给你的数据的PHP版本:

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

这个答案可以很容易被发现的谷歌......

+1

你是为真实? 'json_decode'返回对象,你也有错误。 –

+0

好吧,我不会用这样的问题给他喂食...... – NDM

+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

+0

发布前是否尝试过? –

+0

为了使数组语法正常工作,您需要'true'作为'json_decode'的第二个参数。 – cmbuckley

+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' 
+0

罚款它给出输出中立,但m工作在情绪api和它给了我一个json格式output..im有大量的文本,我需要找到感悟...如何做到这一点?我的意思是任何查询使用json_decode()将它们分组。 – user111111111

+0

@ user2699144请澄清,你的意思是你不知道docSentiment的位置? – MisterBla

+0

@ user2699144你不清楚你在问什么。 – MisterBla