如何使用javascript或jquery从JSON中搜索文本
如何从JSON格式中搜索关键字。使用jquery或javascript 我有一个JSON内容。从这一点我需要搜索并获得搜索关键字的id 关键字:“authority”=>我想把id设为4 关键字:“basic”=>我想把id设为3如何使用javascript或jquery从JSON中搜索文本,javascript,jquery,json,jquery-plugins,Javascript,Jquery,Json,Jquery Plugins,如何从JSON格式中搜索关键字。使用jquery或javascript 我有一个JSON内容。从这一点我需要搜索并获得搜索关键字的id 关键字:“authority”=>我想把id设为4 关键字:“basic”=>我想把id设为3 { "data": { "1":[ { "id":"3", "title":"my title", "content":"this is very basic sample content" }, { "i
{
"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>"
}
]
}
}
{
“数据”:{
"1":[
{
“id”:“3”,
“头衔”:“我的头衔”,
“内容”:“这是非常基本的示例内容”
},
{
“id”:“4”,
“标题”:“我的示例标题和文本”,
“内容”:“由许可证颁发机构续签的许可证可由该官员由州政府签署。”
}
]
}
}
$(文档).ready(函数(){
变量内容={
“数据”:{
"1":[
{
“id”:“3”,
“头衔”:“我的头衔”,
“内容”:“这是非常基本的示例内容”
},
{
“id”:“4”,
“标题”:“我的示例标题和文本”,
“内容”:“由许可证颁发机构续签的许可证可由该官员由州政府签署。”
}
]
}
};
警报(返回内容(“基本”);
函数返回内容(关键字)
{
var returnValue=null;
$.each(内容、数据、函数(键、值){
$.each(值,函数)(键2,值2){
if(value2.content.toString().indexOf(关键字)!=-1)
{
returnValue=value2.id.toString();
}
});
});
返回值;
}
});
您只需在returnContent()函数中更改要查找的关键字如果我有大量内容,则这不是正确的解决方案。。我想
$(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;
}
});