我有像这样的JSON字符串化数据:

时间:2018-11-06 06:14:13

标签: javascript php jquery

{
  "id": "3",
  "type1_id": "2",
  "type2_id": "2",
  "type3_id": "1",
  "number": "10003",
  "number2": "6454646",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "468451",
  "consumer_account_id": null,
  "address": "Subhas Nagar bhopal",
  "date_time": "2018-10-24 16:36:49",
  "latitude": "77.4343394",
  "longitude": "23.2508008"
},
{
  "id": "4",
  "type1_id": "2",
  "type2_id": "2",
  "type3_id": "2",
  "number": "10004",
  "number2": "9956670",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "462345",
  "consumer_account_id": null,
  "address": "Rajendra Nagar bhopal",
  "date_time": "2018-10-24 17:10:40",
  "latitude": "77.42396009999993",
  "longitude": "23.2774624"
},
{
  "id": "5",
  "type1_id": "4",
  "type2_id": "6",
  "type3_id": null,
  "number": "10005",
  "number2": "5058280",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "462323",
  "consumer_account_id": null,
  "address": "Shivaji Nagar bhopal",
  "date_time": "2018-10-24 17:30:00",
  "latitude": "77.42335490000005",
  "longitude": "23.228385"
},
{
  "id": "6",
  "type1_id": "4",
  "type2_id": "6",
  "type3_id": null,
  "number": "10006",
  "number2": "5687456",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "456654",
  "consumer_account_id": null,
  "address": "Hamidia Road bhopal",
  "date_time": "2018-10-31 11:43:25",
  "latitude": null,
  "longitude": null
},

我将如何获取经度和纬度,这是我的javascript

$(document).ready(function(){
  showmap2();
});    


function showmap2()
{

    $.post("https://colonymaintenance.com/55/mpeb/php_ajx/getlanglong.php",
    {
        showmap: ""
    },
    function(data, status){
      var stringify = JSON.stringify(data);

       document.write(stringify);
        for (var i = 0; i < stringify.length; i++) {
            console.log(stringify[i]['latitude']);
        }

    }, "json");    
}

我是初学者,请帮助我

3 个答案:

答案 0 :(得分:0)

您可以在解析数据后直接使用该数据。您无需致电stringify

const data = [{
  "id": "3",
  "type1_id": "2",
  "type2_id": "2",
  "type3_id": "1",
  "number": "10003",
  "number2": "6454646",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "468451",
  "consumer_account_id": null,
  "address": "Subhas Nagar bhopal",
  "date_time": "2018-10-24 16:36:49",
  "latitude": "77.4343394",
  "longitude": "23.2508008"
},
{
  "id": "4",
  "type1_id": "2",
  "type2_id": "2",
  "type3_id": "2",
  "number": "10004",
  "number2": "9956670",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "462345",
  "consumer_account_id": null,
  "address": "Rajendra Nagar bhopal",
  "date_time": "2018-10-24 17:10:40",
  "latitude": "77.42396009999993",
  "longitude": "23.2774624"
},
{
  "id": "5",
  "type1_id": "4",
  "type2_id": "6",
  "type3_id": null,
  "number": "10005",
  "number2": "5058280",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "462323",
  "consumer_account_id": null,
  "address": "Shivaji Nagar bhopal",
  "date_time": "2018-10-24 17:30:00",
  "latitude": "77.42335490000005",
  "longitude": "23.228385"
},
{
  "id": "6",
  "type1_id": "4",
  "type2_id": "6",
  "type3_id": null,
  "number": "10006",
  "number2": "5687456",
  "consumer_name": null,
  "consumer_number": null,
  "consumer_father_name": null,
  "division_code": "456654",
  "consumer_account_id": null,
  "address": "Hamidia Road bhopal",
  "date_time": "2018-10-31 11:43:25",
  "latitude": null,
  "longitude": null
}];

function logLatLon(data, status){
  data.forEach(function(d){
    console.log("lat:", d.latitude + ", lon:", d.longitude);
  })
};
    
logLatLon(data);

答案 1 :(得分:0)

您可以使用jquery的每个函数,如下所示。

    function showMap(){
      function(data, status){
        var stringify = JSON.parse(data);

        jQuery.each(stringify , function(index, value){
            console.log("Lat ",value.latitude + " Long ",value.longitude);
        });

      }, "json");
    }

更新了答案。试试这个。

答案 2 :(得分:0)

请使用 JSON.parse()代替 JSON.stringify()-

$(document).ready(function(){
  showmap2();
});    


function showmap2()
{

    $.post("https://colonymaintenance.com/55/mpeb/php_ajx/getlanglong.php",
    {
        showmap: ""
    },
    function(data, status){
      var stringify = JSON.parse(data);

       document.write(stringify);
        for (var i = 0; i < stringify.length; i++) {
            console.log(stringify[i]['latitude']);
        }

    }, "json");    
}