Home > Blockchain >  How to handle response from JSON but started with numbers
How to handle response from JSON but started with numbers

Time:08-08

how to handle JSON response, I got this response but I don't know how to handle it. I know variable can't started with numbers, so?

let result = [{"1d":{"volume":"22275409068573.73","price_change":"56446.71564507","price_change_pct":"0.0121","volume_change":"-13864857829188.44","volume_change_pct":"-0.3836","market_cap_change":"9216448958327.75","market_cap_change_pct":"0.0121"}}];

How to parsing"1d"?

I try JSON.parse(result[0].1d); but error happened

CodePudding user response:

If you don't want to use ["prop"] to get value, you can change all props that starts with a number like in the example. Check this link

var json = JSON.stringify(result);
json = json.replace(/,"[0-9]|{"[0-9]/g, function (x) {
    return x.substring(0,1) '"num' x.substring(2);
});
result = JSON.parse(json); 
var whatIwant = result.num1d;

CodePudding user response:

You can't use JSON.parse for JSON object.

If you want to get value of 1d. Try this

result[0]["1d"]

  • Related