3

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.

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.. Commented Dec 10, 2016 at 5:30
  • How do you get URL? Commented Dec 10, 2016 at 5:47
  • @guest271314 I retrieve it from a particular data source and keep it in that url. 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. 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 Commented Dec 10, 2016 at 6:25

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

You can simply access the JSON var in js just like using the javascript object variable. you need not have to use the loops.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.