无法读取json文件中的name属性

时间:2017-06-10 06:52:51

标签: jquery ajax

$.ajax({

        url: '/GisMap/GetLayers?layerName=All',
        type: "get",

        dataType:"json" ,

        success: function (data) {
            alert('Data received: ');
            var xmlDoc = data.result;
            console.log(xmlDoc.layers.layer[0].name);
            alert(xmlDoc.layers.layer[0].name);

Json数据来自服务器,但我无法读取特定的name属性值。这是我的Json数据。

{"layers":{"layer":[{"name":"poly_landmarks","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/poly_landmarks.json"},{"name":"Img_Sample","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/Img_Sample.json"},{"name":"tasmania_cities","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_cities.json"},{"name":"tasmania_roads","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_roads.json"},{"name":"tasmania_state_boundaries","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_state_boundaries.json"},{"name":"tasmania_water_bodies","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_water_bodies.json"},{"name":"archsites","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/archsites.json"},{"name":"restricted","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/restricted.json"}]}}

2 个答案:

答案 0 :(得分:0)

使用JSON.parse()



data = '{"layers":{"layer":[{"name":"poly_landmarks","href":"http://localhost:8080/geoserver/rest/layers/poly_landmarks.json"},{"name":"Img_Sample","href":"http://localhost:8080/geoserver/rest/layers/Img_Sample.json"},{"name":"tasmania_cities","href":"http://localhost:8080/geoserver/rest/layers/tasmania_cities.json"},{"name":"tasmania_roads","href":"http://localhost:8080/geoserver/rest/layers/tasmania_roads.json"},{"name":"tasmania_state_boundaries","href":"http://localhost:8080/geoserver/rest/layers/tasmania_state_boundaries.json"},{"name":"tasmania_water_bodies","href":"http://localhost:8080/geoserver/rest/layers/tasmania_water_bodies.json"},{"name":"archsites","href":"http://localhost:8080/geoserver/rest/layers/archsites.json"},{"name":"restricted","href":"http://localhost:8080/geoserver/rest/layers/restricted.json"}]}}';

  data = JSON.parse(data);

console.log(data.layers.layer[0].name);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以这样做:

var json = '{"layers":{"layer":[{"name":"poly_landmarks","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/poly_landmarks.json"},{"name":"Img_Sample","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/Img_Sample.json"},{"name":"tasmania_cities","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_cities.json"},{"name":"tasmania_roads","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_roads.json"},{"name":"tasmania_state_boundaries","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_state_boundaries.json"},{"name":"tasmania_water_bodies","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/tasmania_water_bodies.json"},{"name":"archsites","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/archsites.json"},{"name":"restricted","href":"http:\/\/localhost:8080\/geoserver\/rest\/layers\/restricted.json"}]}}';


var parsed = JSON.parse(json);
var foo = parsed.layers.layer;

for (var i = 0; i < foo.length; i++) {
  console.log(i + '. name: ' + foo[i].name + ', href: ' + foo[i].href);
}

这里是working example