Send integer as string in a JSON

Asked

Viewed 202 times

0

I’m having a problem returning a very large integer value in JSON type:

{“matricula”: 201737909200976697}

What happens is that in return is bringing 201737909200976700.

This occurs with any large 18-digit integer, so I wanted to return this value as a string but in Javascript it is still coming as a numeric value.

How can I make it right?

EDIT 1:

From php comes out like this:

array (  
  0 =>   
  array (
   'mtr_codigo' => '2586',
   'aln_nome' => 'JOSÉ FERNANDO REIS',     
   'mtr_matricula' => '201737909200976697'
  ),
) 

When it arrives in javascript is:

[{"mtr_codigo": 2586, "aln_nome": "JOSÉ FERNANDO REIS", "mtr_matricula": 201737909200976700}]

EDIT 2:

In the Devtools of Chrome in the part Sponse is like this:

{"body":[{"mtr_codigo":2586,"aln_nome":"JOS\u00c9 FERNANDO REIS","mtr_matricula":201737909200976697}],"meta":{"output":[]},"status":{"code":200,"phrase":"OK","type":"success"}}

In the preview part is like this:

 body: [{mtr_codigo: 2586, aln_nome: "JOSÉ FERNANDO REIS", mtr_matricula: 201737909200976700}]
 0: {mtr_codigo: 2586, aln_nome: "JOSÉ FERNANDO REIS", mtr_matricula: 201737909200976700}
   aln_nome: "JOSÉ FERNANDO REIS"
   mtr_codigo: 2586
   mtr_matricula: 201737909200976700
 meta: {output: []}
 status: {code: 200, phrase: "OK", type: "success"}

EDIT 3:

In the method that delivers the result was with

json_encode($result, JSON_NUMBER_CHECK);

I removed this options parameter and returned the numeric value as string correctly.

  • I really could not reproduce this problem, here it works and arrives as string, are you using ajax? If yes, how is your ajax code and the exact php code that returns the json for this ajax?

1 answer

3


Just use quotes for the value:

{"matricula": "201737909200976697"}
  • Do you have access to make changes to the service you provide json ? If yes, and it is Sever-side php, just you cast a string in a way that the output looks like {"matricula": "201737909200976697"}, as the friend above said

  • php comes out like this: ["id" => 2586, "name" => "JOSÉ FERNANDO REIS", "matricula" => "201737909200976697"]. When you enter javascript you are {"id": 2586, "name": "JOSÉ FERNANDO REIS", "matricula": 201737909200976696}

  • Strange, I just tested with PHP returning exactly json_encode(["id" => 2586, "nome" => "JOSÉ FERNANDO REIS", "matricula" => "201737909200976697"]) and in javascript came as string. You can put a snippet of code in the question to help?

  • I went to the method where delivers the answer and makes the JSON::NCODE and was with a JSON_NUMBER_CHECK. I removed it and returned the number as string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.