简体   繁体   English

生成多级JSON

[英]Generating multi-level JSON

I have an array of Objects, each containing a Location and a Links array of indeterminate length. 我有一个对象数组,每个对象包含一个Location和一个不确定长度的Links数组。 How can I create a multilevel JSON object with loops? 如何创建带有循环的多级JSON对象?

The end JSON should look like 结束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 ]

The issue I'm having is how to correctly form the objects. 我遇到的问题是如何正确地形成对象。 The objects that the array contains are defined as 数组包含的对象定义为

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

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

  function getLinks(){
    return links;
  }
}

My current solution is 我当前的解决方案是

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);

However, this is not quite working, as 但是,这不是很有效,因为

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

PHP statement is returning a malformed array where $post.length is seen as an undefined constant. PHP语句返回格式错误的数组,其中$post.length被视为未定义的常量。 I'm assuming that this is due to the incorrect formatting. 我假设这是由于格式错误。

With objects defined above, how can I create a well-formed JSON to be sent to the server? 使用上面定义的对象,如何创建格式正确的JSON以发送到服务器?

The current result of stringify() is 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"}
  ]
]

Also, I'm using $post.length in 另外,我在使用$post.length

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

to iterate over the processed array. 遍历处理过的数组。

The POST request is via a jQuery.ajax() function defined as 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");

  }
});

This should work. 这应该工作。

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);

But why are you making the output JSON so complex? 但是,为什么要使输出JSON如此复杂? A simpler way would be to use something like this: 一种更简单的方法是使用如下所示的内容:

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 ]

What you are doing is creating 'arrays' for single objects. 您正在做的是为单个对象创建“数组”。 Why do that? 为什么这样 If you use this format, the code (subtly) simplifies: 如果使用此格式,则代码(巧妙地)将简化:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM