简体   繁体   中英

JSON Object return undefined

Here is the code of jQuery that alerts undefined .

I need to pass the actual_price or base_price in a variable and it gives the the desired result when I pass it like:

parsed.test[indx].base_price 

Can anyone tell me how can I use this with variable? Thanks in advance

Example

var data = '{"test":[
                 {"base_price" : "10"},
                 {"actual_price" : "20"}
               ]
        }';
var parsed = JSON.parse(data);
var indx = 1;
var str = 'actual_price';
alert(parsed.test[indx].str);

尝试通过变量获取属性时,需要使用括号符号:

alert(parsed.test[indx][str]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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