如何取出json文件里面的数据

1,json类型1

var data = [
  { 
    "id":"1",
    "url":"https://api.server.com/server/sc/area/getOpenAreas",
    "param":""
    },
    { 
    "id":"2",
    "url":"https://api.server.com/server/sc/blackList/updateBlackList",
    "userType":"1",
    "userId":"12",
    "targetId":"1000001"
    },
    { 
    "id":"3",
    "url":"https://api.server.com/server/sc/commodity/getCommodity",
    "shopId":"11111111",
    "page":"",
    "limit":"100"
    }
]
$scope.setApi = function(data){
      var _object = $filter('filter')(data[0]);//取出id为1的json对象
        console.log("----------------_object---------------")
        console.log(_object)
        //[object,object...]
         var s_object = $filter('filter')(_object.url);//[5] 比如说可以取对象里面url中的第5个字符,去掉就可以取出全部的url
         console.log("---------------s_object-------------")
         console.log(s_object)
        //https://api.server.com/server/sc/area/getOpenAreas

     }

2,json类型2

var data = { "results": [
    {
        "id": 1,
        "name": "mikeTest"
    },
    {
        "id": 2,
        "name": "mikeTest2"
    },
    {
        "id": 3,
        "name": "mikeTest"
    }
] }
 var _object = $filter('filter')(data.results, function (i) {return i.id === 2;})[0];
 console.log(_object);
    原文作者:MichaelDuan
    原文地址: https://segmentfault.com/a/1190000003049835
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞