获取JSON数组用纯JS [英] Get Json Array with pure JS

查看:126
本文介绍了获取JSON数组用纯JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个跨域得到JSON阵列,而不使用JQuery。来自跨域的数据是这样的:

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

{
      "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 } },
    ]
}

获取数据后,我需要解析它来获得countrylist名单。

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

我寻找解决这一问题,但示例都是使用jQuery。我不想使用它。正如你看到的,我已经在输入TRANSACTIONID,状态参数。所以,在导入数据后,我需要解析它来获得countrylist阵列。我知道如何分析它,但我不能获得该服务的整个数据。

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.

我需要通过JavaScript方法的数据。该服务器支持检索数据良好。

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

将要采取的步骤是:

1获得整个数据(这里是我卡住了)

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

2 - 解析它作为JSON对象(我知道如何分析数据)

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

我怎样才能得到整个数据,而无需使用任何JQuery的或其他prepared库?

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

推荐答案

您可以使用XMLHtt prequest阿贾克斯和JSON.parse用于解析JSON:)

You can use XMLHttpRequest for ajax and JSON.parse for 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);

您永远不应该使用eval! EVAL是邪恶的!你可以阅读一下 MDN

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

UPD:

如果没有CORS头,你可以使用JSONP。回调= getJSONP 来您的网址,只需&放添加。试试它的jsfiddle: http://jsfiddle.net/VmBF7/3/

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

这篇关于获取JSON数组用纯JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆