我的API返回JSON值,例如

[{"UserName":"xxx","Rolename":"yyy"}]

我分别需要UsernameRoleName值,我尝试过JSON.parse,但它返回了[Object Object],请提前帮助我。

最佳答案

考虑以下:

    var str = '[{"UserName":"xxx","Rolename":"yyy"}]'; // your response in a string
    var parsed = JSON.parse(str); // an *array* that contains the user
    var user = parsed[0];         // a simple user
    console.log(user.UserName);   // you'll get xxx
    console.log(user.Rolename);   // you'll get yyy

09-17 23:40