生成多级JSON [英] Generating multi-level JSON

查看:75
本文介绍了生成多级JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array of Objects, each containing a Location and a Links array of indeterminate length. How can I create a multilevel JSON object with loops?

结束JSON应该看起来像

The end JSON should look like

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

我目前的解决方案是

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语句返回格式错误的数组,其中 $ post.length 被视为未定义的常量。我假设这是由于格式不正确。

PHP statement is returning a malformed array where $post.length is seen as an undefined constant. I'm assuming that this is due to the incorrect formatting.

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

With objects defined above, how can I create a well-formed JSON to be sent to the server?

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"}
  ]
]

此外,我m使用 $ post.length in

Also, I'm using $post.length in

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

迭代已处理的数组。

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

The POST request is via a jQuery.ajax() function defined as

$.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");

  }
});


推荐答案

这应该有效。

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如此复杂?一种更简单的方法是使用这样的东西:

But why are you making the output JSON so complex? 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);

这篇关于生成多级JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆