-2

如何从 javascript 中的 JSON 获取值是我的代码

var obj={"0.5":0.009333, "0.21":0.048667,"0.31":0.070667};
    var value =0.21;
    var p=0;
    for(i=0; i<= obj.length ;i++){
    if(value== obj[i]){                         
           p = obj[i];                   //here i want p=0.048667
           console.log("psr is :"+p);
        }
    }
4

2 回答 2

1

呃...不会只是obj[value]做的伎俩?)

于 2012-05-21T12:23:12.993 回答
0

Javascript 不支持 obj.length。您将不得不使用

for(var index in object) {
    var p = object[index];
    console.log(p);
}

但是当然,如​​果您已经知道您的密钥,那么

console.log(object[key]);

是要走的路。完整的代码变成

var obj = {"0.5":0.009333, "0.21":0.048667,"0.31":0.070667};
var value = "0.21";
var p = obj[value];
console.log(p);
于 2012-05-21T12:27:30.970 回答