如何使用jquery从url解析json数据

时间:2017-01-05 06:53:11

标签: javascript jquery json

我需要从url

解析json数据

http://www.apilayer.net/api/live?access_key=f40041...&format=1

我需要"USDAED"的值并将其分配给输入。

{
  "success":true,
  "terms":"https:\/\/currencylayer.com\/terms",
  "privacy":"https:\/\/currencylayer.com\/privacy",
  "timestamp":1483594992,
  "source":"USD",
  "quotes":{
    "USDAED":3.672802,
    "USDAFN":66.699997,
    "USDALL":129.729996,
    "USDAMD":483.440002,
    "USDANG":1.76987,
    "USDAOA":165.078003,
    "USDARS":16.059999,
  }
}

1 个答案:

答案 0 :(得分:2)

您可以使用$.getJSON()获取数据,.val()输入值;

var URL = "http://www.example.com/...................."
$.getJSON(URL, function(d) {
  console.log(d.quotes.USDAED)
  $('#dummy').val(d.quotes.USDAED)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id='dummy'/>