jqueryajaxjqxhr

jquery jqXHR responseText convert string to JSON


I have some responseText that looks like this:

[{
        "id": null,
        "partId": 0,
        "name": "5e8c8c89-445e-4033-80db-014a00cc055e.jpg",
        "type": "image/png",
        "size": 2276489,
        "progress": "1.0",
        "url": "/Public/Upload/UploadHandler.ashx?id=5e8c8c89-445e-4033-80db-014a00cc055e.jpg",
        "thumbnail_url": null,
        "delete_url": "/Public/Upload/UploadHandler.ashx?id=5e8c8c89-445e-4033-80db-014a00cc055e.jpg",
        "delete_type": "DELETE",
        "error": null
    }
]

but I can not consume that data in my complete function. I have tried converting it to JSON using the $.parseJSON function but although I get an object, I can't access any of the items...

var o = $.parseJSON(jqXHR.responseText);
console.log(o.partId); // returns undefined

Can anyone help me out?

Cheers, r3plica


Solution

  • Your JSON response has an Array at the top level. You need to do:

    console.log(o[0].partId);