如何从JSON响应中提取文本?

时间:2013-11-14 20:22:10

标签: javascript jquery ajax json google-api

我正在尝试使用goo.gl API制作url shortener。但是,当我必须从JSON响应中获取短URL时,我坚持了下来!

在Chrome控制台中输入此代码后

var longURL = "http://stackoverflow.com/questions/ask"
$.ajax({
                url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: '{ longUrl: "' + longURL +'"}',
                dataType: 'json',
                success: function(response) {
                    var result = JSON.parse(response); 
                }
            });

我得到以下ouptut: enter image description here

我发现我的短网址位于resoinseText.id。如何从那里提取它?

1 个答案:

答案 0 :(得分:2)

您无需调用JSON.parse(),因为jQuery在您指定dataType: 'json'时会自动执行此操作。您想要的值将位于id的{​​{1}}属性中。

response
相关问题