Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8113105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:43:18+00:00 2026-06-06T02:43:18+00:00

I have an array of Objects, each containing a Location and a Links array

  • 0

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?

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

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. 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?

The current result of stringify() is

[
  {"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 = 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

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

  }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:43:21+00:00Added an answer on June 6, 2026 at 2:43 am

    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? 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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several string each containing a JSON representation of an array of objects.
I have an array of objects. Each object includes a date value. What's the
I have an array of objects . Each object has an attribute we'll call
I have a 2D array containing all the Piece objects, each an instance of
I have an array of Password objects, each containing a tag , a username
I have an array list comprised of Objects. Each object is comprised of Strings.
Okay, very simple: there is an array containing 3 objects. Each object has a
I have an array of jQuery objects. I'd like to interact with each item
I have an array of objects containing a partId and a quantity . I
In Java, how can I create an array where each element is a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.