2013-08-20 206 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