sameer manek
sameer manek

Reputation: 763

JSON stringify adds quotes "" in the float type data

I am parsing a float data from my controller to a js function via JSON, following is the JS function:

function fetchbal(){
$.ajax({
    url: "/count/ew",
    dataType: "json"
}).success(function(data){
    $('#bal').html(JSON.stringify(data.sum));
});
}

but I am getting an output with quotes around the figure.

I have checked the value returned by the controller, and it is not passing the quotes, so it has to do something with JSON stringify!

for cross checking this is the controller(Symfony):

$repo = $em->getRepository('SystemBundle:Admin');
$user = $repo->findOneBy(array('id'=>$session->get('id')));
$sum = $user->getWallet();
return new JsonResponse(array('sum'=>$sum));

here $sum is fetching a float val from the db (doctrine)

I have also tried this post's solution but it instead stops displaying the value on the page

I don't want quotes to be displayed around the fetched value, any suggestion for that? also ask for more elaboration if you want.

Upvotes: 0

Views: 3310

Answers (1)

Regis Portalez
Regis Portalez

Reputation: 4845

Json stringify adds the quotes, since it's intended to serialize data before sending them to server.

You probably want to invoke json.parse, or even just do nothing, as jquery will parse the json for you.

Upvotes: 5

Related Questions