2017-05-25 26 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结构你会做同样的事情来获得所有的价值。