2014-09-18 88 views
-1

如何从JSON格式搜索关键字。使用jQuery或JavaScript如何使用javascript或jquery从JSON搜索文本

我有一个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>" 
    } 
    ] 
} 
} 
+2

http://stackoverflow.com/questions/5288833/how-to-search-json-tree-with-jquery – Quantico 2014-09-18 17:31:21

回答

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()函数中更改关键字你正在寻找

+0

如果我有巨大的内容,那么这是不是适当的解决方案在..我想 – 2014-10-04 10:11:39

+0

数据量没有在问题中指定... – 2014-10-04 14:37:55