如何在JSON上获得与嵌套对象匹配的所有相同字段

时间:2018-03-08 09:53:57

标签: javascript underscore.js

下面是我的JSON对象。在那我需要过滤叶子作为真实的领域。

[
   {
      "id":1,
      "title":"ASD Headquarters",
      "items":[
         {
            "id":11,
            "title":"San Jose",
            "items":[
               {
                  "id":13,
                  "title":"Jensen Chapman's Team",
                  "items":[
                     {
                        "id":14,
                        "title":"Jimmy John",
                        "leaf":"true"
                     },
                     {
                        "id":15,
                        "title":"Daniel Mills",
                        "leaf":"true"
                     },
                     {
                        "id":16,
                        "title":"Chris Boden"
                     }
                  ]
               }
            ]
         },
         {
            "id":12,
            "title":"Irvine",
            "items":[
               {
                  "id":24,
                  "title":"San Jesus"
               },
               {
                  "id":25,
                  "title":"Fat Albert"
               },
               {
                  "id":26,
                  "title":"Connor McDavid",
                  "leaf":"true"
               }
            ]
         },
         {
            "id":30,
            "title":"San Diego",
            "items":[
               {
                  "id":31,
                  "title":"Duran Duran's Team",
                  "items":[
                     {
                        "id":32,
                        "title":"Amberlynn Pinkerton"
                     },
                     {
                        "id":33,
                        "title":"Tony Mejia"
                     },
                     {
                        "id":34,
                        "title":"Richard Partridge",
                        "leaf":"true"
                     },
                     {
                        "id":35,
                        "title":"Elliot Stabler"
                     }
                  ]
               },
               {
                  "id":40,
                  "title":"Steely Dan's Team",
                  "items":[
                     {
                        "id":36,
                        "title":"Tony Stark"
                     },
                     {
                        "id":37,
                        "title":"Totally Rad"
                     },
                     {
                        "id":38,
                        "title":"Matt Murdock"
                     },
                     {
                        "id":39,
                        "title":"Stan Lee"
                     }
                  ]
               }
            ]
         },
         {
            "id":64,
            "title":"Richard Partridge Stark",
            "leaf":"true"
         }
      ]
   }
]

所需的输出是:

[
   {
      "id":14,
      "title":"Jimmy John",
      "leaf":"true"
   },
   {
      "id":15,
      "title":"Daniel Mills",
      "leaf":"true"
   },
   {
      "id":26,
      "title":"Connor McDavid",
      "leaf":"true"
   },
   {
      "id":34,
      "title":"Richard Partridge",
      "leaf":"true"
   },
   {
      "id":64,
      "title":"Richard Partridge Stark",
      "leaf":"true"
   }
]

1 个答案:

答案 0 :(得分:0)

我用以获取相应数据的功能。



<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demoApp" ng-controller="MainCtrl">
            <pre>{{obj | json}}</pre>
            </div>
&#13;
String exportFileName = ResourceConstants.EXCEL_FILEPATH+"/"+ResourceConstants.EXCEL_FILENAME;

        FileInputStream inf = null;

        response.setContentType("application/vnd.ms-excel");
        response.addHeader("content-disposition",
                "attachment; filename=" + exportFileName);
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
        final int size = 1024;
        try{
            inf = new FileInputStream(exportFileName);
            response.setContentLength(inf.available());
            final byte[] buffer = new byte[size];
            ServletOutputStream os = null;
            os = response.getOutputStream();
            int length = 0;
            while ((length = inf.read(buffer)) != -1) {
                os.write(buffer, 0, length);
            }
            inf.close();
            os.flush();
            os.close();
        } catch (final FileNotFoundException e) {
            LOGGER.error("FileNotFoundException occurred in download excel method!!!"+e.getMessage());
        }
&#13;
&#13;
&#13;