dweb Posted June 19, 2012 Share Posted June 19, 2012 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 More sharing options...
requinix Posted June 19, 2012 Share Posted June 19, 2012 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 More sharing options...
dweb Posted June 19, 2012 Author Share Posted June 19, 2012 thanks, i've read the page but could you give me a simple example of how to extract and display it? i've looked around and finding it hard to find something straight forward and now banging my head against the wall thanks Link to comment https://forums.phpfreaks.com/topic/264415-extending-json/#findComment-1355108 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.