3

我有一个对象数组,每个对象都包含一个位置和一个不确定长度的链接数组。如何创建带有循环的多级 JSON 对象?

最终的 JSON 应该是这样的

item1: [
  { "location": [
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value }
    ],
   "links": [
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value },
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value },
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value }
    ]
  }   
],
item2: [ //repeat of above ]

我遇到的问题是如何正确形成对象。数组包含的对象定义为

function Links(){
  this.location = null;
  this.links= [];

  function getLocation(){
    return location;
  }
  function setLocation(marker){
    this.location = marker;
  } 

  function getLinks(){
    return links;
  }
}

我目前的解决方案是

var json=[];
var linkData;
for (var i=0; i < tourList.length; i++){
  var data = tourList[i];
  //create new child array for insertion
  var child=[];

  //push location marker data
  child.push({
    latitude: data.location.position.$a, 
    longitude: data.location.position.ab,
    stopNum: i,
    filename: data.location.title
  });

  //add associated link data
  for (var j=0; j<data.links.length; j++){
    linkData = data.links[i];
    child.push({
      latitude: linkData.position.$a, 
      longitude: linkData.position.ab,
      stopNum: i+j,
      fileName: linkData.title
    });
  }
  //push to json array
  json.push(child);
}

//stringify the JSON and post results
var results= JSON.stringify(json);

但是,这并不完全有效,因为

$post= json_decode($_POST['json']) 

PHP 语句返回一个格式错误的数组,该数组$post.length被视为未定义的常量。我假设这是由于格式不正确。

使用上面定义的对象,如何创建格式良好的 JSON 以发送到服务器?

目前的结果stringify()

[
  {"latitude":43.682211,"longitude":-70.45070499999997,"stopNum":0,"filename":"../panos/photos/1-prefix_blended_fused.jpg"},
  [
    {"latitude":43.6822,"longitude":-70.45076899999998,"stopNum":0,"fileName":"../panos/photos/2-prefix_blended_fused.jpg"}
  ],
  {"latitude":43.6822,"longitude":-70.45076899999998,"stopNum":1,"filename":"../panos/photos/2-prefix_blended_fused.jpg"},
  [
    {"latitude":43.68218,"longitude":-70.45088699999997,"stopNum":1,"fileName":"../panos/photos/4-prefix_blended_fused.jpg"},
    {"latitude":43.68218,"longitude":-70.45088699999997,"stopNum":2,"fileName":"../panos/photos/4-prefix_blended_fused.jpg"}
  ]
]

另外,我正在$post.length使用

$post = json_decode($POST['json']);
for ($i=0; $i<$post.length; $i++) { }

遍历处理过的数组。

POST 请求是通过jQuery.ajax()定义为的函数

$.ajax({
  type: "POST",
  url: "../includes/phpscripts.php?action=postTour",
  data: {"json":results},
  beforeSend: function(x){
    if (x && x.overrideMimeType){
      x.overrideMimeType("application/json;charset=UTF-8");
    }
  },
  success: function(data){
    if (data == "success")
      console.log("Tour update successful");
    else 
      console.log("Tour update failed");

  }
});
4

2 回答 2

4

这应该有效。

var json = [];
var linkData;
for (var i = 0; i < tourList.length; i++) {
    var data = tourList[i];
    //create new child array for insertion
    var child = [{ }];

    //push location marker data
    child[0]['location'] = [{
        latitude: data.location.position.$a,
        longitude: data.location.position.ab,
        stopNum: i,
        filename: data.location.title
    }];

    child[0]['links'] = [];

    //add associated link data
    for (var j = 0; j < data.links.length; j++) {
        linkData = data.links[i];
        child.links.push({
            latitude: linkData.position.$a,
            longitude: linkData.position.ab,
            stopNum: i + j,
            fileName: linkData.title
        });
    }
    //push to json array
    json.push(child);
}

//stringify the JSON and post results
var results = JSON.stringify(json);

但是为什么要让输出 JSON 如此复杂呢?一个更简单的方法是使用这样的东西:

item1: {
    "location": {
        "latitude": value, "longitude": value, "stopNum": value, "fileName": value
    },
   "links": [
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value },
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value },
      {"latitude": value, "longitude": value, "stopNum": value, "fileName": value }
    ]
},
item2: [ //repeat of above ]

您正在做的是为单个对象创建“数组”。为什么要这样做?如果您使用这种格式,代码(巧妙地)简化了:

var json = [];
var linkData;
for (var i = 0; i < tourList.length; i++) {
    var data = tourList[i];
    //create new child array for insertion
    var child = { };

    //push location marker data
    child.location = {
        latitude: data.location.position.$a,
        longitude: data.location.position.ab,
        stopNum: i,
        filename: data.location.title
    };

    child.links = [];

    //add associated link data
    for (var j = 0; j < data.links.length; j++) {
        linkData = data.links[i];
        child.links.push({
            latitude: linkData.position.$a,
            longitude: linkData.position.ab,
            stopNum: i + j,
            fileName: linkData.title
        });
    }
    //push to json array
    json.push(child);
}

//stringify the JSON and post results
var results = JSON.stringify(json);
于 2012-06-20T15:32:05.463 回答
1

我的朋友,答案是JSON.stringfy()

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify

于 2012-06-20T15:17:08.877 回答