我有一个巨大的JSON数据,如下所示。现在我需要过滤并获取与输入月份相关的json数据的所有属性。
我的JSON数据是:
"maindata" :[
{
"month":"multi",
"category":"coffee",
"price":50,
"name":"Pike Place Roast Brewed Coffee Verismo Pods",
"flavor":"flavored",
"count":5,
"roast":"medium",
"type":"regular"
},
{
"month":"august",
"category":"coffee",
"price":40,
"name":"Starbucks VIA Ready Brew French Roast",
"flavor":"flavored",
"count":548,
"roast":"blonde",
"type":"decaffinated"
},
{
"month":"multi",
"category":"coffee",
"price":50,
"name":"Starbucks Caffé Verona Blend, Whole Bean",
"flavor":"flavored",
"count":5,
"roast":"medium",
"type":"regular"
},
{
"month":"asia-pacific",
"category":"coffee",
"price":20,
"name":"Starbucks Caffè Verona K-Cup Pods",
"flavor":"flavored",
"count":3,
"roast":"dark",
"type":"regular"
},
{
"month":"august",
"category":"coffee",
"price":40,
"name":"Milk Verismo Pods",
"flavor":"flavored",
"count":233,
"roast":"blonde",
"type":"decaffinated"
},
{
"month":"multi",
"category":"coffee",
"price":50,
"name":"Starbucks VIA Ready Brew Decaf Italian Roast",
"flavor":"flavored",
"count":5,
"roast":"medium",
"type":"regular"
},
{
"month":"august",
"category":"coffee",
"price":40,
"name":"Guatemala Antigua Espresso Verismo Pods",
"flavor":"flavored",
"count":587,
"roast":"blonde",
"type":"decaffinated"
}
]
任何人都可以帮助我,我怎样才能使这个工作? 我曾尝试使用JSONParse方法,但仅此一点并不足以达到目的。
答案 0 :(得分:1)
您可以尝试过滤数据集。
此处,过滤器匹配' multi'的输入值。
您可以将其更改为其他值(甚至可以根据用户输入使其动态化)
// sample data
var data = {
"maindata": [{
"month": "multi",
"category": "coffee",
"price": 50,
"name": "Pike Place Roast Brewed Coffee Verismo Pods",
"flavor": "flavored",
"count": 5,
"roast": "medium",
"type": "regular"
}, {
"month": "august",
"category": "coffee",
"price": 40,
"name": "Starbucks VIA Ready Brew French Roast",
"flavor": "flavored",
"count": 548,
"roast": "blonde",
"type": "decaffinated"
}, {
"month": "multi",
"category": "coffee",
"price": 50,
"name": "Starbucks Caffé Verona Blend, Whole Bean",
"flavor": "flavored",
"count": 5,
"roast": "medium",
"type": "regular"
}, {
"month": "asia-pacific",
"category": "coffee",
"price": 20,
"name": "Starbucks Caffè Verona K-Cup Pods",
"flavor": "flavored",
"count": 3,
"roast": "dark",
"type": "regular"
}, {
"month": "august",
"category": "coffee",
"price": 40,
"name": "Milk Verismo Pods",
"flavor": "flavored",
"count": 233,
"roast": "blonde",
"type": "decaffinated"
}, {
"month": "multi",
"category": "coffee",
"price": 50,
"name": "Starbucks VIA Ready Brew Decaf Italian Roast",
"flavor": "flavored",
"count": 5,
"roast": "medium",
"type": "regular"
}, {
"month": "august",
"category": "coffee",
"price": 40,
"name": "Guatemala Antigua Espresso Verismo Pods",
"flavor": "flavored",
"count": 587,
"roast": "blonde",
"type": "decaffinated"
}]
};
// hard-coded - can be set to a dynamic value if need be
var inputMonth = 'multi';
var months = data.maindata.filter(function(elem) {
if (elem.month === inputMonth) {
return elem;
}
});
var results = {};
results['data'] = months;
document.getElementById('myresults').innerHTML = JSON.stringify(results);
//console.log(JSON.stringify(results));

<pre id='myresults'>
</pre>
&#13;