javascript 从 JSON 获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19042387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:10:01  来源:igfitidea点击:

Get parameter from JSON

javascriptjson

提问by Joe Simmons

In datafrom server I get the following JSON:

data从服务器我得到以下 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 upload_url. I'm doing:

我需要得到upload_url。我正在做:

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

but it doesn't work (alert doesn't show).

但它不起作用(警报不显示)。

How do I get parameter upload_url?

我如何获取参数upload_url

采纳答案by Daniel

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

这里有几个正确的答案,但有一个触发器决定您应该如何处理返回的数据。

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

当您使用ajax请求并使用JSON数据格式时,您可以通过两种方式处理数据。

  1. treat your data as JSON when it returns
  2. configure your ajax call for JSON by adding a dataType
  1. 返回时将您的数据视为 JSON
  2. 通过添加 dataType 为 JSON 配置 ajax 调用

See the following examples:

请参阅以下示例:

returned data string:

返回数据字符串:

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

ajax call without dataType:

没有数据类型的ajax调用:

$.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 调用:

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

In your case you probably used JSON.parse() when the call was already configured for JSON. Hope this makes things clear.

在您的情况下,您可能在调用已为 JSON 配置时使用了 JSON.parse()。希望这能让事情清楚。

回答by Joe Simmons

It looks like you need to access arrg, not data. Also you need to access the 'response' key first.

看起来您需要访问 arrg,而不是数据。您还需要首先访问“响应”键。

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

回答by Samuel Arroyo

If response is in json and not a string then

如果响应在 json 而不是字符串中,则

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

回答by Chris Montgomery

Your code has a small error. try:

您的代码有一个小错误。尝试:

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