admin管理员组

文章数量:1287256

I have this Json string that i use for google chart visualization that needs to be in this exact format and i need to replace every value of "v" that is a number to its numeric value( the value without the ""). I should do some javascript replace function, but i couldn't find a way to move around the json object. Here is and example json string that i should modify :

{"cols":[
{"id":"r","label":"Reason","type":"string"},
{"id":"m","label":"Minutes","type":"number"}
],
"rows":[
{"c":[
     {"v":"Flour - Blower","f":"Flour - Blower"},
     {"v":"7","f":"7"}]},
{"c":[
     {"v":"Whole Line - d","f":"Whole Line - d"},
     {"v":"4","f":"4"}]},
{"c":[
     {"v":"Flour - Pipework","f":"Flour - Pipework"},
     {"v":"3","f":"3"}]},
{"c":[
     {"v":"Horseshoe - Belt","f":"Horseshoe - Belt"},
     {"v":"1","f":"1"}]}
],
"p":null
}

probably i should do something like :

var jsonStr = ...;

for (i in jsonStr.rows) {
   for(j in jsonStr[i].c)
   { 
    if (parseInt(jsonStr[i].c[j].v) != 'NaN') {
        jsonStr.rows[i].c[j].v = parseInt(jsonStr.rows[i].c[j].v);
   }
 }

I have this Json string that i use for google chart visualization that needs to be in this exact format and i need to replace every value of "v" that is a number to its numeric value( the value without the ""). I should do some javascript replace function, but i couldn't find a way to move around the json object. Here is and example json string that i should modify :

{"cols":[
{"id":"r","label":"Reason","type":"string"},
{"id":"m","label":"Minutes","type":"number"}
],
"rows":[
{"c":[
     {"v":"Flour - Blower","f":"Flour - Blower"},
     {"v":"7","f":"7"}]},
{"c":[
     {"v":"Whole Line - d","f":"Whole Line - d"},
     {"v":"4","f":"4"}]},
{"c":[
     {"v":"Flour - Pipework","f":"Flour - Pipework"},
     {"v":"3","f":"3"}]},
{"c":[
     {"v":"Horseshoe - Belt","f":"Horseshoe - Belt"},
     {"v":"1","f":"1"}]}
],
"p":null
}

probably i should do something like :

var jsonStr = ...;

for (i in jsonStr.rows) {
   for(j in jsonStr[i].c)
   { 
    if (parseInt(jsonStr[i].c[j].v) != 'NaN') {
        jsonStr.rows[i].c[j].v = parseInt(jsonStr.rows[i].c[j].v);
   }
 }
Share Improve this question edited Dec 5, 2013 at 8:47 dlght asked Dec 4, 2013 at 18:02 dlghtdlght 9261 gold badge18 silver badges37 bronze badges 2
  • instead of parsing the whole array why don't you do the isNaN() and parseInt where you actually want to use these values ? – Uncle Aaroh Commented Dec 4, 2013 at 18:05
  • parseInt for a non-parseable argument returns NaN, not 'NaN'. – Ted Hopp Commented Dec 4, 2013 at 18:17
Add a ment  | 

3 Answers 3

Reset to default 7

Since JSON is effectively a string, why not put the entire string through a global string.replace:

jsonStr = JSON.stringify(jsonStr);
jsonStr = jsonStr.replace(/"v":"(\d+)"/g, '"v":$1');

Jsfiddle demo

Well, the parsing seems okay to me. It's probably not working because you can't really check if a string contains a number or not by paring something with NaN

This is because even NaN === NaN, famously, returns false.

I'd suggest that you use the isNaN method (which does use parseInt internally). So, something like this ought to work

for (i in jsonStr.rows) {
   for(j in jsonStr[i].c)
   { 
    if (!isNaN(jsonStr[i].c[j].v)) {
        jsonStr.rows[i].c[j].v = parseInt(jsonStr.rows[i].c[j].v);
   }
 }

A function that returns string if isNaN else a number:

function convertNumberToInteger(val) {
    if (isNaN(val)) {
        return val;
    } else {
        return parseInt(val);
    }
}

Usage:

convertNumberToInteger("sasdfasdf");
Output: "sasdfasdf"

convertNumberToInteger("3");
Output: 3

And if you really want to parse it you can do a forEach on the JSON object

本文标签: