2015-11-02 3 views
0

Последние три часа я пытался прочитать JSON-файл в java-скрипте для своих картографических карт, которые будут прочитаны службой google-maps.Чтение файла JSON в javascript

я однако, неполное script..i не может получить мой results.Here мой сценарий:

<script> 
function loadJSON(callback) { 

    var xobj = new XMLHttpRequest(); 
     xobj.overrideMimeType("application/json"); 
    xobj.open('GET', 'http://127.0.0.1/irismaps/aitems.php', true); // path to file 
    xobj.onreadystatechange = function() { 
      if (xobj.readyState == 4 && xobj.status == "200") { 
      // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode 
      callback(xobj.responseText); 
      } 
    }; 
    xobj.send(null); 
} 

function init() { 
loadJSON(function(response) { 
    // Parse JSON string into object 
    results = JSON.parse(response); 


       for (var i=0; i<results[0].markers.length; i++) { 
      for (var b=0;b<results[0].markers[i].length;b++) { 

        //this is the object you are looking for 
        console.log(results[0].markers[i]); 
        break; 

      } 



    } 
}); 
} 
</script> 

JSON:

{ 
    "results": [ 
    { 
     "markers": [ 
     { 
      "longitude": "37.66653612499999", 
      "latitude": "55.77875861131171", 
      "title": "Industry LLC", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-82.28813722500001", 
      "latitude": "23.10569357133444", 
      "title": "Garden Care", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-4.792105912500006", 
      "latitude": "37.875266800953554", 
      "title": "Entertainers", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-73.94999999999999", 
      "latitude": "40.65", 
      "title": "Educators Corp.", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-0.8122895000000199", 
      "latitude": "52.0443456", 
      "title": "Rentacar", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "4.879416200000037", 
      "latitude": "45.7783975", 
      "title": "Restaurant Lucia", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "12.481953799999928", 
      "latitude": "41.90567180000001", 
      "title": "The Airport", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-74.0059731", 
      "latitude": "40.7143528", 
      "title": "Advertising Inc.", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "12.7880859375", 
      "latitude": "4.565473550710291", 
      "title": "Car Repairs", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "13.447265625", 
      "latitude": "52.53627304145948", 
      "title": "Accounting Gmbh.", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "39.638671875", 
      "latitude": "24.447149589730845", 
      "title": "Courier & Courier", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "10.7666015625", 
      "latitude": "59.93300042374631", 
      "title": "Your House A.S.", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-1.8896484375", 
      "latitude": "52.482780222078205", 
      "title": "M Supermarkets LLC", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "21.005859375", 
      "latitude": "52.22779941887071", 
      "title": "Healthy Runner S.A.", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-0.8122895000000199", 
      "latitude": "52.0443456", 
      "title": "Rentacar", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-0.8122895000000199", 
      "latitude": "52.0443456", 
      "title": "Restaurant Lucia", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "37.6173", 
      "latitude": "55.755826", 
      "title": "Industrie LLC", 
      "icon": "img/markers/shop.png" 
     }, 
     { 
      "longitude": "-115.20111025", 
      "latitude": "55.80752971122906", 
      "title": "Book Group Inc.", 
      "icon": "img/markers/shop.png" 
     } 
     ] 
    } 
    ] 
} 
+1

Любые ошибки javascript? – castis

+1

В чем проблема с вашим кодом? Каков твой вопрос? –

+0

Я получаю какой-либо результат [0] undefined Я думаю, что это может быть этот блок: for (var i = 0; i

ответ

0

Try:

results.results[0].markers[i] 

Вы добавляете весь файл в объект под названием результатов, который содержит массив называется результаты.

0

Это JSON файл, который вы пытаетесь Загрузка - это просто JavaScript. Дайте вашему объекту такое же имя, как

var myJSONObject = { 
    "foo": "bar" 
}; 

Сохраните его как .js-файл и загрузите его в свой документ.