AngularJS JavaScript解析JSON的一部分

时间:2022-08-26 11:54:38

I have the following JSON. I have a parameter to give and I need to return all the number.

我有以下JSON。我有一个参数,我需要返回所有数字。

If I give "BUS", I need to return1,38,44,58

如果我给“BUS”,我需要返回1,38,44,58

Example of JSON:

JSON示例:

{  
   _id:"23456789",
   result:[  
      [  
         "Car",
         [  
            [  
               2,
               3,
               4,
               6
            ],
            [  
               3,
               4,
               444,
               123
            ],
            [  
               43,
               34,
               91446,
               344473
            ]
         ]
      ],
      [  
         "Bus",
         [  
            [  
               1,
               38,
               4458,
               0981
            ]
         ]
      ],
      [  
         "Moto",
         [  
            [  
               5,
               43,
               41440,
               804444
            ]
         ]
      ]
   ]
}

This is my code:

这是我的代码:

    var coordinates = [];
    console.log("tag :"+tag); // tag is the parameter "Bus", "Car" or "Moto"
    $http.get('http://myserver:1234/bbox/'+id)
                    .success(function (response) {
                var point = {};
                // Don't know how to catch a specific word (i.e Car or BUS or Moto)
                for (var i in response){
                    var pointName =  response.result[i][0];
                    coordinates.push(response.result[i][1]);
                    points[pointName] = coordinates;
                }

    })
    .error(function (response) {
        console.log(response);
     });

tag is already set with only one parameter. Just need to return the coordinate for one given.

tag已经只设置了一个参数。只需要返回一个给定的坐标。

Thanks for your help!

谢谢你的帮助!

1 个解决方案

#1


2  

This is pretty strange structure for JSON response, but you can still extract necessary data. For example using Array.prototype.filter method:

对于JSON响应,这是非常奇怪的结构,但您仍然可以提取必要的数据。例如,使用Array.prototype.filter方法:

var coordinates = response.result.filter(function(el) {
    return el[0] === tag;
})[0][1][0];

For tag equal to "Bus" above code will give you [1, 38, 4458, 981] array.

对于等于“Bus”的代码,代码将为您提供[1,38,4458,981]数组。

#1


2  

This is pretty strange structure for JSON response, but you can still extract necessary data. For example using Array.prototype.filter method:

对于JSON响应,这是非常奇怪的结构,但您仍然可以提取必要的数据。例如,使用Array.prototype.filter方法:

var coordinates = response.result.filter(function(el) {
    return el[0] === tag;
})[0][1][0];

For tag equal to "Bus" above code will give you [1, 38, 4458, 981] array.

对于等于“Bus”的代码,代码将为您提供[1,38,4458,981]数组。