Rashed Jhony
Rashed Jhony

Reputation: 133

How can I display without double quote json array data in jquery

I replace the double quote "" from my json_encode result.

$json_string = json_encode($data);
$json_string = str_replace('"','',$json_string);
echo $json_string;

And I fetch data using for loop. code like this:

$("#gnumbers").change(function() { 
    var id = $(this).find(":selected").val();
    var dataString = 'grpid='+ id;    
    $.ajax({                
        url: 'getGroupNumbers.php',
        dataType: "json",
        data: dataString,  
        cache: false,
        success: function (data) {  
            var i, l;
            for (i = 0, l = data.length; i < l; i++) { 
                $("#num").append(data[i].cnumber + ",");
                //console.log(data[i].cnumber);
            }
        } 
    });
});

I checked console.log return nothing but network result something like this: [{cnumber:8801752992444,group_id:3},{cnumber:8801795978968,8801936761915,group_id:3}]

Without str_replace i can display data correctly. Even you can see I append multiple rows with comma separated. My question is how can i display this output result? Please help. Thanks

Upvotes: 0

Views: 426

Answers (3)

praty
praty

Reputation: 1121

Try

$json_string = json_encode($data);
echo $json_string

And then in the success callback,

$('#num').append(data.map(function(d) {
  return d.cnumber;
}).join(','));

As Brad pointed out, don't replace the double quotes on the server side.

Upvotes: 0

Brad
Brad

Reputation: 163234

I replace the double quote "" from my json_encode result.

This is not a smart thing to do.

You're basically destroying any sense of a useful serialization format. The quotes are required. How else would you reliably separate the data from the structure? How will you differentiate between a comma in a string and a comma as a control character telling you when the next field is to start? This isn't as simple as doing a string replace. If you want to invent your own format... I suppose you could do that... but you must take these things into consideration.

You should stick to formats that already exist and are well tested. JSON is a widely compatible format, which is why it's so heavily used. If for some reason you need something more compact, consider CBOR or similar.

How can I display without double quote json array data in jquery

If you wanted to display something differently, you wouldn't wreck your data server-side like you're doing now... you'd simply reformat it for display later.

It's acceptable to remove those quotes client-side in your JavaScript. It's a completely horrible idea to do it server-side where you still need to get useful structured data to the client.

Upvotes: 2

Alexey
Alexey

Reputation: 999

valid JSON format requires keys to be strings in double quotes. When you do str_replace and remove double quotes, you JSON becomes invalid and browser cannot parse it

If you want to convert cnumber from string to int then you should do it before passing $data to json_encode

Upvotes: 1

Related Questions