weixin_33744854 2015-07-31 14:35 采纳率: 0%
浏览 24

无法将Json转换为jQuery

This is my Ajax response from database :

{
    "docs": [{
        "_id":"5be81e62-f91d-4185-bef5-2eabdf048578",
        "_rev":"6-171639a97982fd7d04a81ed070b2e752",
        "profile_id":"5be81e62-f91d-4185-bef5-2eabdf048578",
        "name":"omar"
    }],
    "bookmark": "g2wAAAABaANkAB9kYmNvcmVAZGI1LmplbmV2ZXIuY2xvdWRhbnQubmV0bAAAAAJuBAAAAADAbgQA_____2poAkY_8AAAAAAAAGEBag"
}

I want to convert it to jQuery for Example :

var JsonData = jQuery.parseJSON(responseData.Value);
alert(JsonData.docs._id);

But the alert message show : undefined Need your help please and thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33724059 2015-07-31 14:39
    关注

    docs is an array. An arrays store multiple docs in a single variable.

    You need to tell the runtime which doc you want to attain the _id property of.

    In your case there is only one doc so you can access the first using an index initializer:

    alert(JsonData.docs[0]._id);
    

    The [0] part pulls the first doc from the array.

    评论

报告相同问题?