【问题标题】:How to search text from JSON using javascript or jquery如何使用 javascript 或 jquery 从 JSON 中搜索文本
【发布时间】:2014-11-13 03:54:50
【问题描述】:

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

我有一个 JSON 内容。从中我需要搜索并获取搜索关键字的 id。

Keyword: "authority" => 我想得到 id 为 4

Keyword: "basic" => 我想把 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>"
    }
  ]
 }
}

【问题讨论】:

标签: javascript jquery json jquery-plugins


【解决方案1】:
        $(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() 函数中更改您要查找的关键字

【讨论】:

  • 如果我有大量内容,那么这不是正确的解决方案.. 我认为
猜你喜欢
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 2017-11-23
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多