如何从JSON格式搜索关键字。使用jquery或javascript
我有一个JSON内容。从那里我需要搜索并获取搜索关键字的ID。
关键字:"权威" =>我希望id为4
关键字:"基本" =>我希望得到id为3
{
"data": {
"1":[
{
"id":"3",
"title":"my title",
"content":"this is very basic sample content"
},
{
"id":"4",
"title":"My sample title and text",
"content":"renewed by a licensing authority may be signed by such officer by the State Government.<p></p>"
}
]
}
}
答案 0 :(得分:0)
$(document).ready(function(){
var content = {
"data": {
"1":[
{
"id":"3",
"title":"my title",
"content":"this is very basic sample content"
},
{
"id":"4",
"title":"My sample title and text",
"content":"renewed by a licensing authority may be signed by such officer by the State Government.<p></p>"
}
]
}
};
alert(returnContent("basic"));
function returnContent(keyword)
{
var returnValue = null;
$.each(content.data, function(key, value){
$.each(value, function(key2, value2){
if(value2.content.toString().indexOf(keyword) != -1)
{
returnValue = value2.id.toString();
}
});
});
return returnValue;
}
});
您只需在returnContent()函数中更改您要查找的关键字