admin管理员组

文章数量:1297011

I am new to javascript and I have a Json Array in a url like below;

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

I want to access the state1 value and assign that value to the variable which I have.How can I do that?(Is parsing necessary or are there any methods there that we can directly access the JSON object in the URL).I am sorry if I asked anything wrong.

I am new to javascript and I have a Json Array in a url like below;

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

I want to access the state1 value and assign that value to the variable which I have.How can I do that?(Is parsing necessary or are there any methods there that we can directly access the JSON object in the URL).I am sorry if I asked anything wrong.

Share Improve this question asked Dec 10, 2016 at 5:26 RKRRKR 6873 gold badges13 silver badges27 bronze badges 9
  • 2 Use ajax to load json from url, and parse the response using JSON.parse() method. If you are experiencing any problem with that let us know how did you write your code for that. We could help you correct it.. – Sen Jacob Commented Dec 10, 2016 at 5:30
  • How do you get URL? – guest271314 Commented Dec 10, 2016 at 5:47
  • @guest271314 I retrieve it from a particular data source and keep it in that url. – RKR Commented Dec 10, 2016 at 6:18
  • @RKR Do you mean actual URL, or variable? The javascript array at Question is a not a JSON string. – guest271314 Commented Dec 10, 2016 at 6:21
  • 1 @guest271314 Actual URL it will be like localhost:8080/dataurl. I want to access the data in the URL.I think getJSON is the required one – RKR Commented Dec 10, 2016 at 6:25
 |  Show 4 more ments

3 Answers 3

Reset to default 3

You can use jQuery getJSON() function :

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

Now, Below are the methods to parse the jsonObj to get the state1.

Using array map() method :

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);

using for...in loop :

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);

The data you have is array of objects and you need to loop to access each object and access the key using dot notation

Like this,

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);
}

Just use the following JS Code:

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();
  });
};

You can now use the function to get the JSON contents from the url:

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.');
});

本文标签: javascriptHow to access a JSON array directly from urlStack Overflow