所以我正在尝试使用Javascript和JSON并尝试从JSON转储中检索信息。我对JavaScript的经验非常有限,但我很好奇如何使用这些给定的查询过滤结果(查询和响应取自IBM Watson的Discovery Demo)。
查询
{
"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 :(得分: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结构的结果都会为获取所有值而做同样的事情。