如何直接从url访问JSON数组

时间:2016-12-10 05:26:21

标签: javascript json

我是javascript的新手,我在下面的网址中有一个Json数组;

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
    	<title>To Do</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
	</head>
	<body>
		<h2>To Do</h2>
		<form name="checkListForm">
			<input type="text" name="checkListItem"/>
		</form>
		<div id="button">Add!</div>
		<br/>
		<div class="list"></div>
	</body>
</html>

我想访问state1值并将该值赋给我拥有的变量。我该怎么办?(解析是否必要或者是否有任何方法可以直接访问URL中的JSON对象)。如果我说错了,我很抱歉。

3 个答案:

答案 0 :(得分:4)

您可以使用jQuery getJSON()函数:

$.getJSON(' localhost:8080/dataurl', function(data) {
  //data is the JSON string
  var jsonObj = JSON.parse(data);    
});

现在,以下是解析jsonObj以获取state1的方法。

使用数组map()方法:

&#13;
&#13;
var jsonObj = [
                {
                  "year":2015,
                  "month":4,
                  "day":1,
                  "num":0,
                  "time":"2015-04-01",
                  "hour":0,
                  "zone":3,
                  "state1":2137,
                  "state2":249,
                  "state3":1810,
                  "state4":30,
                  "state5":0
                },
                {
                  "year":2016,
                  "month":12,
                  "day":1,
                  "num":0,
                  "time":"2015-04-01",
                  "hour":0,
                  "zone":3,
                  "state1":2474,
                  "state2":250,
                  "state3":1811,
                  "state4":31,
                  "state5":0
                }
              ];

var state1arr = jsonObj.map(function(item) {
  return item.state1;
});

console.log(state1arr);
&#13;
&#13;
&#13;

使用for...in循环:

&#13;
&#13;
var jsonObj = [
                {
                  "year":2015,
                  "month":4,
                  "day":1,
                  "num":0,
                  "time":"2015-04-01",
                  "hour":0,
                  "zone":3,
                  "state1":2137,
                  "state2":249,
                  "state3":1810,
                  "state4":30,
                  "state5":0
                },
                {
                  "year":2016,
                  "month":12,
                  "day":1,
                  "num":0,
                  "time":"2015-04-01",
                  "hour":0,
                  "zone":3,
                  "state1":2474,
                  "state2":250,
                  "state3":1811,
                  "state4":31,
                  "state5":0
                }
              ];

var state1arr = [];
for (var i in jsonObj) {
  state1arr.push(jsonObj[i].state1);
}

console.log(state1arr);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需使用以下JS代码:

var getJSON = function(url) {
  return new Promise(function(resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
     if (status == 200) {
        resolve(xhr.response);
      } else {
        reject(status);
      }
    };
    xhr.send();
  });
};

您现在可以使用该函数从url获取JSON内容:

getJSON("<URL>").then(function(data) { // Replace <URL> With your URL
    var jsondata = data.result; //store result in variable

    // Your code here....///
    ///  Now you can access the json's data using jsondata variable:  //
    // jsondata[0].year will have the value of year key, jsondata[0].month will have month key and so on.... //

}, function(status) { //error detection....
  alert('Something went wrong.');
});

答案 2 :(得分:0)

您拥有的数据是对象数组,您需要循环访问每个对象并使用点表示法访问密钥

像这样,

var app=[{"year":2015,"month":4,"day":1,"num":0,"time":"2015-04-01","hour":0,"zone":3,"state1":2137,"state2":249,"state3":1810,"state4":30,"state5":0}];
for(var i of app)
{
console.log(i.state1);
}