从jQuery GET JSON响应字符串中获取单个值

时间:2016-02-04 16:50:07

标签: javascript jquery json geolocation

我已经查看了一些类似的主题并尝试了这些建议,但我无法使其发挥作用。

在此页面上http://www.thefuelcardpeople.co.uk/test-pump-locator/我有一个按钮,可以从地理位置数据中获取纬度和长坐标,然后连接到https://api.postcodes.io/postcodes API进行反向邮政编码查找。

我已经让它返回完整的数据字符串,但它显示了这一点,但我无法确定如何返回邮政编码值。我想使用API​​从地理位置数据返回的邮政编码值预填充表单字段输入。

任何人都可以帮忙吗?

这是我的代码:

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>
<p id="demo2"></p>
<pre id="geocode-postcode-result" class="code-box" style="display: block;"></pre>

<script>
var x = document.getElementById("demo");
var y = document.getElementById("demo2");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);



    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    var lon = position.coords.longitude;
    var lon2 = lon.toFixed(14);
    var lat = position.coords.latitude;
    var lat2 = lat.toFixed(14);

    x.innerHTML = "Latitude: " + lat2 + "<br>Longitude: " + lon2;   

        var $result = jQuery("#geocode-postcode-result");
        var displayJsonResult = function ($context, data) {
        $context.html(JSON.stringify(data, null, 4)).slideDown();

    }       
        jQuery.get("https://api.postcodes.io/postcodes?lon=" + lon2 + "&lat=" + lat2 + "&limit=1")
        .done(function (data) {
            displayJsonResult($result, data);

        })
        .fail(function (error) {
            displayJsonResult($result, error.responseJSON);
        });

}


</script>

感谢您的回复 - 撰写的回复是:

{
    "status": 200,
    "result": [
        {
            "postcode": "BB11 2DB",
            "quality": 1,
            "eastings": 384278,
            "northings": 432561,
            "country": "England",
            "nhs_ha": "North West",
            "longitude": -2.2401189327649,
            "latitude": 53.7891288271951,
            "parliamentary_constituency": "Burnley",
            "european_electoral_region": "North West",
            "primary_care_trust": "East Lancashire Teaching",
            "region": "North West",
            "lsoa": "Burnley 003D",
            "msoa": "Burnley 003",
            "incode": "2DB",
            "outcode": "BB11",
            "distance": 30.904269682,
            "admin_district": "Burnley",
            "parish": "Burnley, unparished area",
            "admin_county": "Lancashire",
            "admin_ward": "Daneshouse with Stoneyholme",
            "ccg": "NHS East Lancashire",
            "nuts": "East Lancashire",
            "codes": {
                "admin_district": "E07000117",
                "admin_county": "E10000017",
                "admin_ward": "E05005155",
                "parish": "E43000093",
                "ccg": "E38000050",
                "nuts": "UKD46"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:1)

假设您提供的字符串是通话中返回的data的全部内容,您只需执行以下操作:

data.result[0].postcode

这将为您提供一个仅代表邮政编码的字符串,您可以随意使用。