0

I am trying to get the array data in a JSON response. The following is my JSON response and I need to get date and stallNo from the data array.

{
    "operation": "AnodeSet",
    "stageMap": {
        "stop": 5, 
        "adders": 4
        // ...
    },
    "anodeProb": [
        { 
            "name": "hello", 
            "index" : "hii" 
        }
    ],
    "data" : [
        { 
            "operation" : "AnodeSet", 
            "stallNo: 7", 
            "date" : "21/12/2015" 
        }
    ]
}

6 Answers 6

2

If there is always only 1 element returned in the data array you can retrieve it by index:

var date = json.data[0].date;
var stallNo = json.data[0].stallNo;

If there could be multiple elements in the array you would need to loop over them:

for (var i = 0; i < json.data.length; i++) {
    var date = json.data[i].date;
    var stallNo = json.data[i].stallNo;

    // use the above values as required...
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are searching for angularjs solution then you can use angular.forEach:

angular.forEach(json.data, function(item, key) {
  console.log(item.stallNo);
  console.log(item.date);
});

use the above one if you have more than one objects in your response. Although works for single objec too.

Comments

0

I guess it's very simple

suppose response in an object

var object={
    "operation": "AnodeSet",
    "stageMap": {
        "stop": 5, 
        "adders": 4
        // ...
    },
    "anodeProb": [
        { "name": "hello", "index" : "hii" }
    ],
    "data" : [
        { "operation" : "AnodeSet", "stallNo: 7", "date" : "21/12/2015" }
    ]
}

Then you need to track your requirement:-

object-->

       -->data(array)

                   -->index[0,1,2.....n]

                                      -->Keys you want.

var date = object.date[0].date;
var stallNo = object.date[0].stallNo;

Comments

0

You can use the method from underscore.js like below, Link for reference

 _.pluck(yourObject.data, 'stallNo');

 _.pluck(yourObject.data, 'date');

Output will be [7,10,12, ...], [21/12/2015, ...](until length of your data object)

Comments

0

I assumed your JSON is not parsed string.

var JSONstr='{\
    "operation": "AnodeSet",\
    "stageMap": {\
        "stop": 5, \
        "adders": 4\
    },\
    "anodeProb": [\
        { \
            "name": "hello", \
            "index" : "hii" \
        }\
    ],\
    "data" : [\
        { \
            "operation" : "AnodeSet", \
            "stallNo": 7, \
            "date" : "21/12/2015" \
        }\
    ]\
}';
var obj = JSON.parse(JSONstr);
console.log(obj.data[0].date, obj.data[0].stallNo);

You can use my sandbox for check to check how it works: http://jsbin.com/xugahatiwo/1/edit?js,console

Greets

Comments

0
angular.forEach(json.data, function(value) {
      console.log(value.date);
      console.log(value.stallNo);
});

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.