2011-11-17 111 views
1

我有问题,可否帮帮我! 我有一个JSON数组:我如何通过ID获取json数据中的数据

"category" : [ 
{ 
    id: 1, 
    product: [{id : product_1, type : ball}] 
}, 
{ 
    id : 2, 
    product :[{id : product_2, type : pen}] 
} 
] 

我的问题是:如果我有一个链接,如:http://test/category/1那么,我该怎么办使用ID = 1类获得的产品信息? 感谢您的任何建议:)

回答

7

假设你的数据结构是这样的:

var data = { 
    "category" : [ 
    { 
     id: 1, 
     product: [{id : product_1, type : ball}] 
    }, 
    { 
     id : 2, 
     product :[{id : product_2, type : pen}] 
    } 
    ] 
} 

然后,你可以通过这样的阵列搜索找到类别阵列ID == 1项:

function findId(data, idToLookFor) { 
    var categoryArray = data.category; 
    for (var i = 0; i < categoryArray.length; i++) { 
     if (categoryArray[i].id == idToLookFor) { 
      return(categoryArray[i].product); 
     } 
    } 
} 

var item = findId(data, 1); 
// item.id, item.type 
+1

谢谢!它的工作原理:D – JaclBlack

+2

其旧。但为什么不标记为答案?工作完美。 – Tony

2

如果您保存类别的数组中categories JS变量,您可以筛选基于简单的JavaScript(没有jQuery的)这个数组这样的:

var result = categories.filter(function(element){ 
    if (element.id == 2){ 
     return true; 
    } else { 
     return false; 
    } 
}); 

然后在result[0].product您将有一系列属于特定类别的产品。

请参阅this jsfiddle作为证明。

PS。 filter()方法在所有浏览器中都不可用,您可能需要应用一些代码,以便在需要时找到并找不到。以下是关于filter()的详细信息,以及您需要使其在旧版浏览器上工作的代码的一些关于Mozilla开发人员网络的文章:MDN: filter()

0

我想OP想要从url检索相应的数据。

所以第一步是将网址映射到标识符

var urlParts = window.location.href.split(/\//) 
//window.location.href = http://domain.com/category/1 
// urlParts = ['http:', '', 'domain.com', 'category', '1'] 
var id = urlParts.pop(); 
var category = urlParts.pop(); 

然后你retrive从JSON字符串数据与JSON.parse

var data = JSON.parse('{"category" : [{"id": 1, "product": [{"id" : "product_1", "type" : "ball"}] }, {"id" : 2, "product" :[{"id" : "product_2", "type" : "pen"}] } ]}'); 

//The data structure 
// {"category" : [ 
// {"id": 1, "product" : 
//  [ 
//  {"id" : "product_1", "type" : "ball"} 
//  ]}, 

// {"id" : 2, "product" : 
//  [ 
//  {"id" : "product_2", "type" : "pen"} 
//  ]} 
// ]} 

最后,你可以retrive数据类似@ jfriend00的发布与idcategory

function findId(data, idToLookFor) { 
    var categoryArray = data.category; 
    for (var i = 0; i < categoryArray.length; i++) { 
     if (categoryArray[i].id == idToLookFor) { 
      return(categoryArray[i].product); 
     } 
    } 
} 

var item = findId(data.category, id); 
// item.id, item.type 
3

这是一个办法做到这一点的一条线,并没有使用:

function filterById(jsonObject, id) {return jsonObject.filter(function(jsonObject) {return (jsonObject['id'] == id);})[0];} 

然后,如果你想要得到的,例如,使用id = 2的对象,你只需使用:

var selectedObject = filterById(yourJson['category'], 2); 

然后selectedObject会得到这个值,按您的例子:

{ 
    id : 2, 
    product :[{id : product_2, type : pen}] 
} 

有一个缺点:老的浏览器,如IE8,不支持过滤器(),所以你需要添加旧浏览器的代码兼容性: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter#Compatibility

0

你可能已经找到了答案,但如果你没有,你可以简单地使用逻辑OR||)运算符。

就你而言,你可以使用jQuery的ajax()方法。要抓住你的产品基于id s只需使用以下简单的逻辑:

var prodID1 = 1, prodID2 = 2; 
if(this.id == prodID || this.id == prodID2) { 
    //write your JSON data, etc. 
    //push to array, etc. 
}