如何阅读http回复?

时间:2019-06-20 00:15:08

标签: javascript json

我只想在div中打印http响应的一个字段。

这是我的http请求:

var HttpClient = function() {
  this.get = function(aUrl, aCallback) {
    var anHttpRequest = new XMLHttpRequest();
    anHttpRequest.onreadystatechange = function() {
      if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
        aCallback(anHttpRequest.responseText);
    }
    anHttpRequest.open("GET", aUrl, true);
    anHttpRequest.send(null);
  }
}
var theurl = 'https://geocoder.cit.api.here.com/6.2/geocode.json?searchtext=200%20S%20Mathilda%20Sunnyvale%20CA&app_id="MYAPPID"&app_code="MYAPPCODE"&gen=8';
var client = new HttpClient();
client.get(theurl, function(response) {

  var response1 = JSON.parse(response);
  //alert(response);

});

我知道要打印Div中的响应,我必须使用:

 document.getElementById('test').innerHTML = [...];

但是我不知道该如何读字段:"Latitude"

这是整个http响应:

{
    "Response": {
        "MetaInfo": {
            "Timestamp": "2019-06-19T23:20:14.504+0000"
        },
        "View": [{
            "_type": "SearchResultsViewType",
            "ViewId": 0,
            "Result": [{
                "Relevance": 1.0,
                "MatchLevel": "houseNumber",
                "MatchQuality": {
                    "State": 1.0,
                    "City": 1.0,
                    "Street": [0.9],
                    "HouseNumber": 1.0
                },
                "MatchType": "pointAddress",
                "Location": {
                    "LocationId": "NT_nL.dzNwdSJgdcF4U8dYEiC_yADM",
                    "LocationType": "address",
                    "DisplayPosition": {
                        "Latitude": 37.37634,
                        "Longitude": -122.03405
                    },
                    "NavigationPosition": [{
                        "Latitude": 37.37641,
                        "Longitude": -122.03445
                    }],
                    "MapView": {
                        "TopLeft": {
                            "Latitude": 37.3774642,
                            "Longitude": -122.0354646
                        },
                        "BottomRight": {
                            "Latitude": 37.3752158,
                            "Longitude": -122.0326354
                        }
                    },
                    "Address": {
                        "Label": "200 S Mathilda Ave, Sunnyvale, CA 94086, United States",
                        "Country": "USA",
                        "State": "CA",
                        "County": "Santa Clara",
                        "City": "Sunnyvale",
                        "District": "Heritage District",
                        "Street": "S Mathilda Ave",
                        "HouseNumber": "200",
                        "PostalCode": "94086",
                        "AdditionalData": [{
                            "value": "United States",
                            "key": "CountryName"
                        }, {
                            "value": "California",
                            "key": "StateName"
                        }, {
                            "value": "Santa Clara",
                            "key": "CountyName"
                        }, {
                            "value": "N",
                            "key": "PostalCodeType"
                        }]
                    }
                }
            }]
        }]
    }
}

0 个答案:

没有答案