简体   繁体   中英

Reformatting JSON data into a Javascript Object

I have a set of JSON data that I'm trying to reformat to follow the javascript object model depicted on 'Timeline JS'.

This is the original JSON data which is stored in the variable "messages":

  {
   "user_id": 1,
   "subject_line": "Timeam feugiat invidunt",
   "type": "feugiat",
   "date": "2014,07,5",
   "message": "Butcher letterpress et tousled ea. Id do artisan"
  },
  {
    "user_id": 1,
    "subject_line": "RE: Timeam feugiat invidunt",
    "type": "feugiat",
    "date": "2014,07,7",
    "message": "Butcher letterpress et tousled ea."   
  }

The JSON data would need to be reformatted into the following Javascript object syntax:

      timeline: {
        headline: "User 1", // messages.user_id 
        type: "default",
        text: "tousled",     
        date: [{
          startDate: "2014,07,5", //messages.date
          headline: "Timeam feugiat invidunt" //messages.subject_line
         },
         {
          startDate: "2014,07,7",
          headline: "RE: Timeam feugiat invidunt"
       }]
    }

This is my current code:

function(){
var messagesData;

// API data fetched and stored in 'messages'
messages.each(function(data){
  data = data.toJSON();
  messagesData = {
    timeline: {
      headline: "User " + data.user_id,
      type: "default",
      text: "tousled",     
      date: [{
        startDate: data.date,
        headline: data.subject_line
      }]
    }
  }
});  

}

However the data I'm getting back only returns one set of the 'date' array (which is the last date and subject line of the JSON data):

timeline: {
        headline: "User 1",
        type: "default",
        text: "tousled",     
        date: [{
          startDate: "2014,07,7",
          headline: "RE: Timeam feugiat invidunt"
         }]
    }

How would you loop through the JSON data so it'll return all given set of date and subject line?

Hi please see here http://plnkr.co/edit/twLsdzFZHmCwMvzwrux6?p=preview that should help you.

var messages = [{
   "user_id": 1,
   "subject_line": "Timeam feugiat invidunt",
   "type": "feugiat",
   "date": "2014,07,5",
   "message": "Butcher letterpress et tousled ea. Id do artisan"
  },
  {
    "user_id": 1,
    "subject_line": "RE: Timeam feugiat invidunt",
    "type": "feugiat",
    "date": "2014,07,7",
    "message": "Butcher letterpress et tousled ea."   
  }];

var storyjs_data = {
    "timeline": {
      "headline": "User" + messages[0].user_id,
      "type": "default",
      "text": "tousled",
      "date": []
    }
  };

  $(messages).each(function(indx, message) {

    var _date = {
      "startDate": message.date,
      "headline": message.subject_line,

    };

    storyjs_data.timeline.date.push(_date);

  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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