2013-09-21 61 views
-1

我有此JSON文件:先進的JavaScript 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", 
    "relations": [ 
     { 
      "subject": { 
       "text": "new single", 
       "sentiment": { 
        "type": "positive", 
        "score": "0.179956" 
       } 
      }, 
      "action": { 
       "text": "axed", 
       "lemmatized": "axe", 
       "verb": { 
        "text": "axe", 
        "tense": "past" 
       } 
      }, 

      "location": { 
       "text": "in figure-hugging dress", 
       "entities": [ 
        { 
         "type": "City", 
         "text": "figure-hugging" 
        } 
       ] 
      } 
     } 
    ] 
} 

我想在關係數組中的主體對象中的情緒對象的情緒型「積極的」什麼是正確的JavaScript代碼來定位這個值我試過了

回答

0
var json = JSON.parse(... your_loaded_json ...); 

alert(json.relations[0].subject.sentiment.type); 

請注意你在實際的JSON上調用JSON.parse。如果這只是JavaScript腳本中的一個對象,那麼只需:

var json = { ... your_object ... } 

alert(json.relations[0].subject.sentiment.type);