0

I have the following json structure:

 [
   {
      "model":"todolist.todolist",
      "pk":1,
      "fields":{
         "timezoneChoice":"Africa/Johannesburg",
         "user":44,
         "name":"ergdsg",
         "discription":"dsgdsg",
         "dueMinutes":-61504892,
         "emailSent":false,
         "dueDate":"1899-12-11T08:20:00Z",
         "create_at":"2016-11-21T04:37:29.253Z"
      }
   },
   {
      "model":"todolist.todolist",
      "pk":2,
      "fields":{
         "timezoneChoice":"Africa/Johannesburg",
         "user":44,
         "name":"ergdsg",
         "discription":"dsgdsg",
         "dueMinutes":-61504892,
         "emailSent":false,
         "dueDate":"1899-12-11T08:20:00Z",
         "create_at":"2016-11-21T04:38:12.525Z"
      }
   }
]

I need to get the values like this

for (var i = 0; i < json.length; i++) {

                    $("#posts").prepend( "<div >" +"<h2 class='bg-info   text-align-center'> Tasks" +"</h2>"
                                         + "<strong> Name: " + " "  + json[i].name + "</strong>"
                                         + "<strong> Task: " + " "  + json[i].discription + "</strong>"
                                         + "<strong> Due Date: " + " "  + json[i].dueDate + "</strong>"
                                         + "<strong> Mins before due: " + " "  + json[i].dueMinutes + "</strong>"
                                         + "<strong> TimeZone: " + " "  + json[i].timezoneChoice + "</strong>"
                                         + "<hr>" +"</div>")
                                },

How can I loop through the json to get the "fields" keys dictionary and then access the keys within and thier value. How do I do this for the whole jason list?

Thank you

IronAces
  • 1,857
  • 1
  • 27
  • 36
Curious Lambda
  • 537
  • 2
  • 7
  • 21
  • 1
    By the time you're looping through it, it isn't JSON; it's just an array of objects. That should help with your searching. JSON is a *textual notation* for data exchange [(more here)](http://stackoverflow.com/a/2904181/157247). If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Nov 21 '16 at 11:55
  • *"How can I loop through the json to get the "fields" keys dictionary and then access the keys within and thier value."* It's an object. See the linked question's answers to see how to loop through an object's properties. – T.J. Crowder Nov 21 '16 at 11:56
  • Something like `Object.keys(data).map(key => { console.log(data[key]) })` may be also way to go. – Michał Werner Nov 21 '16 at 11:59

0 Answers0