如何使用Javascript或JQuery搜索巨大的JSON

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Javascript或JQuery搜索巨大的JSON相关的知识,希望对你有一定的参考价值。

在我的应用程序中,Web服务返回JSON文件的大型数据集。这是我的JSON字符串的一部分。

[
       {
          "Value":"21",
          "Province":"Default",
          "Branches":[
             {
                "Value":"1108",
                "Province":"Davie",
                "IsValid":"False"
             },
             {
                "Value":"1107",
                "Province":"Ab area109",
                "IsValid":"False"
             },
             {
                "Value":"1105",
                "Province":"Hollywood",
                "IsValid":"False"
             }
          ]
       },
       {
          "Value":"17",
          "Province":"East",
          "Branches":[
             {
                "Value":"212",
                "Province":"area109",
                "IsValid":"False"
             },
             {
                "Value":"219",
                "Province":"area116",
                "IsValid":"False"
             }
          ]
       },
       {
          "Value":"24",
          "Province":"East11",
          "Branches":[
             {
                "Value":"211",
                "Province":"area108",
                "IsValid":"False"
             },
             {
                "Value":"218",
                "Province":"area109",
                "IsValid":"False"
             },
             {
                "Value":"1102",
                "Province":"area999",
                "IsValid":"False"
             }
          ]
       },
       {
          "Value":"25",
          "Province":"hilton25",
          "Branches":[
             {
                "Value":"213",
                "Province":"area110",
                "IsValid":"False"
             },
             {
                "Value":"220",
                "Province":"area999",
                "IsValid":"False"
             }
          ]
       }
]

我需要搜索省名并获取匹配的所有节点,直到其父节点。这意味着我需要按Root级别和Branche级别搜索provine名称。

例如1.当搜索qazxsw poi时,应该返回以下JSON。

key = Default

例如2.当搜索qazxsw poi时,应该返回以下JSON。

[
       {
          "Value":"21",
          "Province":"Default",
          "Branches":[
             {
                "Value":"1108",
                "Province":"Davie",
                "IsValid":"False"
             },
             {
                "Value":"1107",
                "Province":"Ab area109",
                "IsValid":"False"
             },
             {
                "Value":"1105",
                "Province":"Hollywood",
                "IsValid":"False"
             }
          ]
       }
]

我怎么能通过使用javascript或JQuery来做到这一点。请帮我。

答案

在JavaScript中,您可以调用key = area110将该输入解析为JavaScript对象数组。从那里开始,使用一对for循环非常简单。

[
    {
      "Value":"25",
      "Province":"hilton25",
      "Branches":[
         {
            "Value":"213",
            "Province":"area110",
            "IsValid":"False"
         }
      ]
    }
]

自从我在JS开发以来已经有一段时间了,所以如果这可能更清洁,请告诉我!

另一答案

试试这个,希望它有所帮助。

JSON.parse()
另一答案

一种可能的解决方案是使用function searchProvince(prov) { var result = []; var json; // JSON string (or you can pass it in as a function parameter) /* * if you don't already have the JSON string, get it here */ var objects = JSON.parse(json); // parse JSON string as JavaScript Object array for (obj of objects) { // if the base Province matches, take the whole object if (obj.Province === prov) { result.push(obj); } else { // otherwise, only take the branches that have a matching Province var toAdd = {Value: obj.Value, Province: obj.Province, Branches: []}; for (branch of obj.Branches) { if (branch.Province === prov) { toAdd.Branches.push(branch); } } // only add the copy if any branches were matched if (toAdd.Branches.length) { result.add(toAdd); } } } return JSON.stringify(result); // returns the result as a JSON string // can also return result if you want the array of JavaScript Objects } 仅保留满足下列条件之一的let inp = [ { "Value":"21", "Province":"Default", "Branches":[ { "Value":"1108", "Province":"Davie", "IsValid":"False" }, { "Value":"1107", "Province":"Ab area109", "IsValid":"False" }, { "Value":"1105", "Province":"Hollywood", "IsValid":"False" } ] }, { "Value":"17", "Province":"East", "Branches":[ { "Value":"212", "Province":"area109", "IsValid":"False" }, { "Value":"219", "Province":"area116", "IsValid":"False" } ] }, { "Value":"24", "Province":"East11", "Branches":[ { "Value":"211", "Province":"area108", "IsValid":"False" }, { "Value":"218", "Province":"area109", "IsValid":"False" }, { "Value":"1102", "Province":"area999", "IsValid":"False" } ] }, { "Value":"25", "Province":"hilton25", "Branches":[ { "Value":"213", "Province":"area110", "IsValid":"False" }, { "Value":"220", "Province":"area999", "IsValid":"False" } ] } ]; var key = prompt("Please enter the key"); // Your search key var result =[] for(d in inp){ if(inp[d]['Province']==key) result.push(inp[d]) for(d1 in inp[d]['Branches']) if(inp[d]['Branches'][d1]['Province']==key) result.push(inp[d]) } console.log(result)

  • A)Array.filter()objects属性与相关搜索的Province相匹配。
  • B)object阵列中的一些key与搜索到的object匹配。

如果满足条件B),我们还使用Branches来保持key数组中匹配的filter()