javascript 纯JS获取Json数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20743186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:11:19  来源:igfitidea点击:

Get Json Array with pure JS

javascriptarraysjsonjsp

提问by Mark Karan

I would like to get JSON Array from a cross domain without using JQuery. The data from cross domain is like this:

我想在不使用 JQuery 的情况下从跨域获取 JSON 数组。跨域的数据是这样的:

{
      "transactionid": "LBS_48550801",
      "status": 0,
      "countrylist": 
    [
   { "id": 1, "name": "France", "latitude": 37.00039, "longitude": 35.31411, "extent": { "minlatitude": 36.53888499, "minlongitude": 34.76786904, "maxlatitude": 38.37851496, "maxlongitude": 36.40534884 }},
   { "id": 2, "name": "Germany", "latitude": 37.76454, "longitude": 38.27645, "extent": { "minlatitude": 37.40771898, "minlongitude": 37.43326512, "maxlatitude": 38.21203404, "maxlongitude": 39.24767592 } },
   //... .... .... 
   //... .... .... 
   //... .... .... 
   { "id": 98, "name": "Belgium", "latitude": 38.75754, "longitude": 30.5387, "extent": { "minlatitude": 37.78126803, "minlongitude": 29.68963308, "maxlatitude": 39.27915099, "maxlongitude": 31.71549708 } },
   { "id": 99, "name": "England", "latitude": 39.71812, "longitude": 43.03345, "extent": { "minlatitude": 38.96877501, "minlongitude": 42.28340184, "maxlatitude": 40.031208, "maxlongitude": 44.61092892 } },
    ]
}

After getting the data, I need to parse it to get "countrylist" list.

获取数据后,我需要对其进行解析以获取“国家/地区列表”列表。

I searched for solving this issue but the examples are all with JQuery. I dont want to use it. As you see, I have transactionid, status parameters in the input. So, after importing the data, I need to parse it to get the countrylist array. I know how to parse it, but I couldnt get the whole data from the service.

我搜索了解决此问题的方法,但示例均使用 JQuery。我不想使用它。如您所见,我在输入中有 transactionid、status 参数。因此,在导入数据后,我需要对其进行解析以获取 countrylist 数组。我知道如何解析它,但我无法从服务中获取全部数据。

I need to get the data by Javascript methods. The server supports retrieving data well.

我需要通过 Javascript 方法获取数据。服务器很好地支持检索数据。

The steps to be taken are:

要采取的步骤是:

1- Get the whole data (Here is where I got stuck)

1-获取整个数据(这是我卡住的地方)

2- Parse it as Json Object (I know how to parse the data)

2- 将其解析为 Json 对象(我知道如何解析数据)

How can I get the whole data without using any JQuery or other prepared libraries?

如何在不使用任何 JQuery 或其他准备好的库的情况下获取整个数据?

回答by kmakarychev

You can use XMLHttpRequest for ajax and JSON.parse for parse json :)

您可以将 XMLHttpRequest 用于 ajax 和 JSON.parse 用于解析 json :)

var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '//example.org', true);
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if(xmlhttp.status == 200) {
            var obj = JSON.parse(xmlhttp.responseText);
            var countryList = obj.countrylist;
         }
    }
};
xmlhttp.send(null);

You should never use eval! Eval is evil! You can read about it on mdn

你永远不应该使用 eval!Eval是邪恶的!你可以在mdn上阅读它

UPD:

更新:

If there is no CORS header, you can use JSONP. Just add &callback=getJSONPto your URL. Try it on jsfiddle: http://jsfiddle.net/VmBF7/3/

如果没有 CORS 标头,则可以使用 JSONP。只需添加&callback=getJSONP到您的网址。在 jsfiddle 上试试:http: //jsfiddle.net/VmBF7/3/

回答by denolk

since your operation involving cross domain requests, i suggest you to use jsonp request.

由于您的操作涉及跨域请求,我建议您使用 jsonp 请求。

this questionhas what you wanted.

这个问题有你想要的。

回答by shellbye

In my case, since the server require a ajaxrequest or it will give you a 404(to prevent csrf), so based on what the accepted answer, you need one extra line to complete the getJSONajax version like this:

在我的情况下,由于服务器需要一个ajax请求或者它会给你一个 404(以防止csrf),所以根据接受的答案,你需要额外的一行来完成这样的getJSONajax 版本:

<script type="application/javascript">
    var request = new XMLHttpRequest();
    request.open('GET', '/your/url', true);
    // this following line is needed to tell the server this is a ajax request
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    request.onload = function () {
    if (this.status >= 200 && this.status < 400) {
            var json = JSON.parse(this.response);
            // this is where you can do something with the json response
        }
    };
    request.send();
    });
</script>

回答by Jorge_B

Could you try to download the data to a hidden iframe and then parse the content of its document.body?

您能否尝试将数据下载到隐藏的 iframe,然后解析其 document.body 的内容?