挑选json响应的某个部分

时间:2017-04-16 16:24:09

标签: javascript json

在向https://api.imgflip.com/caption_image发送POST请求后 - 我收到类似的回复:

{
  "success": true,
  "data": {
    "url": "http://i.imgflip.com/1nciey.jpg",
    "page_url": "https://imgflip.com/i/1nciey"
  }
}

我如何console.log()来自响应的url部分的字符串?

1 个答案:

答案 0 :(得分:0)

响应字符串为JSON格式。

JavaScript可以通过调用JSON.parse()来解析JSON字符串。

// str = the response string
var parsed_json = JSON.parse(str);

变量parsed_json现在将是一个JavaScript对象,您可以使用它来引用所需的属性,例如网址:

console.log('Response URL: ', parsed_json.data.url);