0

I'm building a json array and I need the structure of the json data to look like the following example below. Specifically, I need "success": 1, and "result": [ ] to wrap around the objects in the array only ONCE. However, my code outputs the json string with {"success":1,"result":[{ objects }} before the start of each array.

here's my code in action. http://plnkr.co/edit/vziEecjJWUid2qtcJkp9?p=preview

view your console.log to see where it's looping through the "success": 1, and "result": [ ] at the start of each array.

example of what I need.

{  
   "success":1,
   "result":[
      {  
         "id":"01",
         "title":"awesome title",
         "url":"someurl.com",
         "class":"event",
         "start":"8:30 AM",
         "startTime":"8:30 AM",
         "endTime":"5:00 PM",
         "EventDate":"5:00 PM"
      },
      {  
         "id":"01",
         "title":"awesome title",
         "url":"someurl.com",
         "class":"event",
         "start":"8:30 AM",
         "startTime":"8:30 AM",
         "endTime":"5:00 PM",
         "EventDate":"5:00 PM"
      }
 ]
}

My current json structure (no good):

{"success":1,"result":[{  
         "id":"01",
         "title":"awesome title",
         "url":"someurl.com",
         "class":"event",
         "start":"8:30 AM",
         "startTime":"8:30 AM",
         "endTime":"5:00 PM",
         "EventDate":"5:00 PM"
      }] }

{"success":1,"result":[{  
         "id":"01",
         "title":"awesome title",
         "url":"someurl.com",
         "class":"event",
         "start":"8:30 AM",
         "startTime":"8:30 AM",
         "endTime":"5:00 PM",
         "EventDate":"5:00 PM"
      }] }
1
  • you declare your variable arr as object in var arr = {"success":1, result:[myObject ]}; change it to var arr = [] Commented Jul 16, 2015 at 2:12

2 Answers 2

1

JSON.stringify() is your friend in this case. Just use stringify() and you will be doing the standard thing. Assemble your object just as you show it.

If you need specific text formatting other than stringify()'s default (which is to be compact), you can use parameters to stringify() for item-specific replacement functions. I suggest a check of the MDN docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Sign up to request clarification or add additional context in comments.

Comments

0

put your arr below of for loop.

and print final arr after for loop scope.

Try like this

var arr = {
    "success": 1,
    result: []
};


var entryLenght = result.feed.entries.length;
// console.log("Number of posts: " + entryLenght);
for (var i = 0; i < entryLenght; i++) {
    //console.log("Loop: " + i);
    var entry = result.feed.entries[i];
    var myObject = {
        id: "01",
        title: entry.title ,
        url: entry.link,
        class: "event-warning",
        start: "8:30am",
        startTime: "8:30am",
        endTime: "5:30pm"
    };
    //console.log(myObject);

    arr.result.push(myObject); // push your prepared object to arr.result

    var html = '<li>' +
        '<h3>' + entry.title + '</h3>' +
        '<h4>' + entry.categories[0] + '</h4>' +
        '<p>Info: ' + entry.contentSnippet + '</p>' +
        //'<p>' + startTime + ' - '+ endTime + '</p>'+
        '</li>';
    container.append(html);

}
var json = JSON.stringify(arr);
console.log(json);

PLUNKR

1 Comment

bingo! I was so close.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.