2017-05-25 30 views
0

因此,我使用Javascript和JSON來試圖從JSON轉儲中檢索信息。我對JavaScript的使用經驗非常有限,但我很好奇如何用這些給定的查詢來過濾結果(從IBM Watson的發現演示中查詢和響應)。從多條件的Json轉儲中獲取信息

查詢

{ 
    "count": 5, 
    "return": "title,enrichedTitle.text,url,host,blekko.chrondate", 
    "query": "\"uci\",language:english", 
    "aggregations": [ 
    "nested(enrichedTitle.entities).filter(enrichedTitle.entities.type:Company).term(enrichedTitle.entities.text)", 
    "nested(enrichedTitle.entities).filter(enrichedTitle.entities.type:Person).term(enrichedTitle.entities.text)", 
    "term(enrichedTitle.concepts.text)", 
    "term(blekko.basedomain).term(docSentiment.type)", 
    "term(docSentiment.type)", 
    "min(docSentiment.score)", 
    "max(docSentiment.score)", 
    "filter(enrichedTitle.entities.type::Company).term(enrichedTitle.entities.text).timeslice(blekko.chrondate,1day).term(docSentiment.type)" 
    ], 
    "filter": "blekko.hostrank>20,blekko.chrondate>1490425200,blekko.chrondate<1495695600" 
} 

響應

{ 
    "companies": [ 
    { 
     "key": "Reuters", 
     "matching_results": 23 
    }, 
    { 
     "key": "Amgen Tour", 
     "matching_results": 14 
    }, 
    { 
     "key": "Qatar Airways", 
     "matching_results": 13 
    }, 
    { 
     "key": "AMC", 
     "matching_results": 10 
    }, 
    { 
     "key": "British Cycling", 
     "matching_results": 10 
    }, 
    { 
     "key": "HSBC UK", 
     "matching_results": 9 
    }, 
    { 
     "key": "Track Cycling Worlds", 
     "matching_results": 9 
    }, 
    { 
     "key": "Univision", 
     "matching_results": 8 
    }, 
    { 
     "key": "Giro", 
     "matching_results": 6 
    }, 
    { 
     "key": "BMC", 
     "matching_results": 5 
    } 
    ], 
    "people": [ 
    { 
     "key": "George Bennett", 
     "matching_results": 15 
    }, 
    { 
     "key": "Vogel", 
     "matching_results": 12 
    }, 
    { 
     "key": "Chris Taylor", 
     "matching_results": 11 
    }, 
    { 
     "key": "Brent Bookwalter", 
     "matching_results": 10 
    }, 
    { 
     "key": "Rachel Atherton", 
     "matching_results": 10 
    }, 
    { 
     "key": "Barker", 
     "matching_results": 9 
    }, 
    { 
     "key": "Russell Westbrook", 
     "matching_results": 9 
    }, 
    { 
     "key": "John Coates", 
     "matching_results": 8 
    }, 
    { 
     "key": "Tracey Gaudry", 
     "matching_results": 8 
    }, 
    { 
     "key": "Laura Kenny", 
     "matching_results": 7 
    } 
    ], 
    "topics": [ 
    { 
     "key": "University of California, Irvine", 
     "matching_results": 44 
    }, 
    { 
     "key": "Amgen", 
     "matching_results": 43 
    }, 
    { 
     "key": "Tour of California", 
     "matching_results": 43 
    }, 
    { 
     "key": "Track cycling", 
     "matching_results": 42 
    }, 
    { 
     "key": "Bicycle", 
     "matching_results": 39 
    }, 
    { 
     "key": "Giro d'Italia", 
     "matching_results": 37 
    }, 
    { 
     "key": "World cup competition", 
     "matching_results": 37 
    }, 
    { 
     "key": "Control premium", 
     "matching_results": 36 
    }, 
    { 
     "key": "Irvine, California", 
     "matching_results": 36 
    }, 
    { 
     "key": "Mergers and acquisitions", 
     "matching_results": 33 
    } 
    ] 
} 

我猜測,響應部分來自JSON轉儲和查詢從中獲取信息。我試圖編寫一些代碼來過濾掉類似於上面列出的查詢的內容。我必須使用什麼格式從JSON轉儲中提取信息?

我試過四處尋找答案,但似乎這個查詢只是它周圍的代碼的一部分(更具體地說只是執行計算的代碼)而不是實際設置。

是否有我應該使用的方法?

我有JSON對象存儲到一個變量atm,它包含所有需要的信息。

回答

0

假定給定的JSON數據在var atm = {your json};中,並且您想從數據訪問公司和主題array

陣公司使用

alert(atm.companies); 

和數組訪問主題使用

alert(atm.topics); 

和陣列的人訪問訪問使用:

alert(atm.people); 

您的公司和主題是array。例如,要從array獲得價值,您需要使用[]並使用其中一個獲取公司內部的所有值。

根據您的變量atm內的響應(從JSON數據),從你的JSON得到的所有值做到這一點:

公司:

for (var i = 0; i < atm.companies.length; i++) { 
    console.log("Keys: " + atm.companies[i].key); 
    console.log("Matching Results: " + atm.companies[i].matching_results); 
} 

主題:

for (var i = 0; i < atm.topics.length; i++) { 
    console.log("Keys: " + atm.topics[i].key); 
    console.log("Matching Results: " + atm.topics[i].matching_results); 
} 

而且每個結果都帶有相同的JSON結構你會做同樣的事情來獲得所有的價值。