2017-06-10 19 views
1
$.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データはサーバーから送信されていますが、特定の名前属性値を読み取ることができません。これは私のJsonデータです。はjsonファイルのname属性を読み取ることができません

{"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"}]}} 
+0

私はエラー捕捉されない例外TypeErrorを取得しています:火(jquery.js:3317)で [resolveWithなど] Object.fireWithで (:Object.success(48 map.js)で未定義 のプロパティ '層' を読み込めませんjquery.js:3447) at done(jquery.js:9272) at XMLHttpRequest (jquery.js:9514) – Nitesh

+1

data = JSON.parse(data); console.log(data.layers.layer [0] .name); – JYoThI

答えて

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>

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です。

関連する問題