简体   繁体   English

如何处理来自 JSON 的响应,但从数字开始

[英]How to handle response from JSON but started with numbers

how to handle JSON response, I got this response but I don't know how to handle it.如何处理 JSON 响应,我收到了此响应,但我不知道如何处理。 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"}}];让结果 = [{"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"?如何解析“1d”?

I try JSON.parse(result[0].1d);我尝试 JSON.parse(result[0].1d); but error happened但发生了错误

You can't use JSON.parse for JSON object.您不能将JSON.parse用于 JSON object。

If you want to get value of 1d .如果你想获得1d的价值。 Try this尝试这个

result[0]["1d"]结果[0][“1d”]

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.如果您不想使用 ["prop"] 来获取值,则可以更改所有以数字开头的道具,如示例中所示。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM