1

My ajax request looks like this:

$.post('newsletter.php?mode=grab', { search: searchstring }, function(data) {
    $('#newsletter_receivers').html(
            data
    );
});

Output from this is: (data)

{"id":"111","fullname":"Test Test","age":"31"}{"id":"112","fullname":"Max Max","age":"31"}{"id":"113","fullname":"123 123","age":"31"}{"id":"114","fullname":"Det Fungerar","age":"31"}

Now this is just putting this json in the div element for now.

But how can i extract each json array and output the fullname and age?

So it will appear as:

Test Test - 31
Max max - 31
0

4 Answers 4

1

In case your valid json looks like

[{"id":"111","fullname":"Test Test","age":"31"},{"id":"112","fullname":"Max Max","age":"31"},{"id":"113","fullname":"123 123","age":"31"},{"id":"114","fullname":"Det Fungerar","age":"31"}]

you can use

$.post('newsletter.php?mode=grab', { search: searchstring }, function(data) {
    data = $.parseJSON(data);
    $.each(data, function(index,b){
       $('#newsletter_receivers').append('<br>'+b.fullname+' - '+b.age);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

this just saves the last iteration as the html, because you are overwrting it at every iteration! :)
@genesis tried this, does not work, it doesnt output anything and when it has 2 json arrays it gives me undefined - undefinedundefined-undefined . I tried to do prepend/append() so it dont overwrite, but still nothing. I tried alert(); nothing either
@Karem: try it now. Your json IN OP is not valid
0

I corrected your json wichi is not valid. You could do:

var json = [
    {
    "id": "111",
    "fullname": "Test Test",
    "age": "31"},
{
    "id": "112",
    "fullname": "Max Max",
    "age": "31"},
{
    "id": "113",
    "fullname": "123 123",
    "age": "31"},
{
    "id": "114",
    "fullname": "Det Fungerar",
    "age": "31"}
];
var text = '';
$.each(json, function(index, b) {
    text += b.fullname + ' - ' + b.age + '<br/>';
});

$('#newsletter_receivers').html(text);

fiddle here:

http://jsfiddle.net/4crnm/

for your example:

$.post('newsletter.php?mode=grab', { search: searchstring }, function(data) {
    data = $.parseJSON(data);
    var text = '';
    $.each(data, function(index, b) {
        text += b.fullname + ' - ' + b.age + '<br/>';
    });
    $('#newsletter_receivers').html(text);
});

Comments

0

That's not valid JSON. You'll need to turn it into an array or something. If you can't do it server side then try this:

$.post('newsletter.php?mode=grab', { search: searchstring }, function(data){
    $('#newsletter_receivers').html('');
    $.each($.parseJSON('['+data.replace(/\}\{/g, '},{')+']'), function(){
        $('#newsletter_receivers').append('<div>'+this.fullname+' - '+this.age+'</div>');
    });
});

If you can fix it server side that'd be a lot better of a solution.

Comments

0

change your jason to something like

{
    "data": [
        {
            "id": "111",
            "fullname": "Test Test",
            "age": "31"
        },
        {
            "id": "112",
            "fullname": "Max Max",
            "age": "31"
        },
        {
            "id": "113",
            "fullname": "123 123",
            "age": "31"
        },
        {
            "id": "114",
            "fullname": "Det Fungerar",
            "age": "31"
        }
    ]
}

and access it like,

$(function(){
 $.post("path/to/json",function(data){
  $(data.data).each(function(i,j){
  alert(data.data[i].fullname);
  });
  },'json');
});

whne you specify the dataType as json you dont need to do $.parseJSON also you can verify your json at www.jsonlint.com

Comments

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.