Jump to content

extending json


dweb

Recommended Posts

Hi

 

I wonder f you can help as i've been racking my brain all night and cannot find a solution

 

I have the following JSON in a file

 

{
            "user1": 'David',
            "user2": 'Julie',
            "user3": 'John'
}

 

and I output it with

 

$.getJSON("files/json.txt", function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li>' + key + '<br>' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo("#"+divname);
});

 

all works fine, but I need to extend the JSON so that it looks like the following

 

{
            "id": "14373",
            "name": 'David',
            "age": '25',
            "gender": 'm',
            "id": "14373",
            "name": 'Julie',
            "age": '31',
            "gender": 'f',
            "id": "65643",
            "name": 'John',
            "age": '42',
            "gender": 'm'
}

 

and then output it as something like;

 

  $.each(data, function(id_val, name_val, age_val, gender_val) {
    items.push('<li>' + id_val + '<br>' + name_val + '<br>' + age_val + '<br>' + gender_val + '</li>');
  });

 

is this possible and if so, how can it be done as im really struggling to find a way

 

thanks everyone

Link to comment
https://forums.phpfreaks.com/topic/264415-extending-json/
Share on other sites

Can't reuse keys like that. You need an outer array along the lines of

[{
"id": 14373,
"name": "David",
"age": 25,
"gender": "m"
}, {
"id": 14373,
"name": "Julie",
"age": 31,
"gender": "f"
}, {
"id": 65643,
"name": "John",
"age": 42,
"gender": "m"
}]

With that done, start using each() properly.

Link to comment
https://forums.phpfreaks.com/topic/264415-extending-json/#findComment-1355051
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.