Convert a JSON string to a selective-valued object

I have a line:

"{0:16970861903381446063,length:1}"

I tried converting it to an object using a method eval, but it also evaluates the string and therefore rounds the numeric value, returning:

{0:16970861903381447000,length:1}

I tried passing the number as a string before calling evalon it, using 16970861903381446063 + ''JSON as the value when creating the string; checking it with the help typeofshows that it has a type string, but it still rounds the number 16970861903381446063 to 16970861903381447000.

Is there a way around this or is it better to do this?

Below is the code that generates json text from an array containing numbers

function simplify(arr){
      request = "{";
      if (arr.length ==1){
         request += 0 +  ":" + (arr[0] + '')  + "," ; 
      }
      else{
         for (var i=0;i<=arr.length-1  ;i++){

        request +=  i +  ":" +  (arr[i] + '')  + ",";
     }
    }
   request += "length" + ":" + arr.length +"}";
   return request;
 }
+3
source share
4 answers

IEEE , 53 11 .

Javascript, , 64- . : . Javascript 53 , - 11 .

, , , 3.5346367e + 22, 3.5246367, 22.

, , , , .

, , .

, JSON JSON, , , length:

// Do this
json = '["16970861903381446063"]';

// Not this
json = '{0:"16970861903381446063", length:1}';
+3

, :

{
    0: "16970861903381446063",
    length: 1
}

, - ; ?

+3

, . , , ?

+2

freenode

 function simplify(arr){
       request =  "{";
       if (arr.length ==1){
          request += ( 0 +  ":" + "'" + String(arr[0])+ "'" + ",") ; 
      }
       else{
          for (var i=0;i<=arr.length-1  ;i++){
             request +=  i +  ":" + "'" + String(arr[i]) + "'"  + ",";
      }

     }
      request += "length" + ":" + arr.length +"}" ;
return request;
    }

eval, , , eval , eval() 'ed.

{0:'16970861903381446063',1:'16970861903381444012',length:2}
0

Source: https://habr.com/ru/post/1712501/


All Articles