Get parameter from JSON

In datafrom the server, I get the following JSON:

{
    "response": {
        "upload_url": "http:\/\/cs9458.vk.com\/upload.php?act=do_add&mid=6299927&aid=-14&gid=0&hash=73e525a1e2f4e6a0f5fb4c171d0fa3e5&rhash=bb38f2754c32af9252326317491a2c31&swfupload=1&api=1&wallphoto=1",
        "aid": -14,
        "mid": 6299927
    }
}

I need to get one upload_url. I do:

function (data) {
    var arrg = JSON.parse(data);
    alert(data.upload_url);
});

but it does not work (warning is not displayed).

How to get the parameter upload_url?

+4
source share
4 answers

There are some correct answers here, but there is one trigger that decides how you should handle the returned data.

When you use an ajax request and use the JSON data format, you can process the data in two ways.

  • treat your data as JSON when returning
  • configure your ajax call for JSON by adding dataType

See the following examples:

data line:

{"color1":"green","color2":"red","color3":"blue"}

ajax call without dataType:

$.ajax({
    method: "post",
    url: "ajax.php",
    data: data,
    success: function (response) {
        var data = JSON.parse(response);
        console.log(data.color1); // renders green
        // do stuff 

    }
});

ajax call with dataType:

$.ajax({
    method: "post",
    url: "ajax.php",
    dataType: "json", // added dataType
    data: data,
    success: function (response) {
        console.log(response.color1); // renders green
        // do stuff 
    }
});

, , JSON.parse(), JSON. , .

+1

, arrg, . "".

function (data) {
    var arrg = JSON.parse(data);
    alert( arrg.response.upload_url);
}
+6

If the answer is in json and not in a string, then

alert(response.id);

or

alert(response['id']);

otherwise

var response = JSON.parse('{"id":"2231f87c-a62c-4c2c-8f5d-b76d11942301"}');
response.id ; //# => 2231f87c-a62c-4c2c-8f5d-b76d11942301
+1
source

There is a small error in your code. try:

function (data) {
   var arrg = JSON.parse(data);
   alert(arrg.response.upload_url);
});
0
source

All Articles