0

Can anybody help me to get data from the json below.I have get a json data in the format below and in this json you can see that there is "{0}" in each record.So my question is how i can get data from this format or is there any way to remove "{0}" from the json.

[{
    "ChkValue": "ChkValue",
    "Description": "Description",
    "Mode": "Mode"
}, {
    "0": {
        "ChkValue": "false",
        "Description": "Made sure guards are in place on machine",
        "Mode": "Eliminate"
    }
}, {
    "0": {
        "ChkValue": "false",
        "Description": "Use Liveguard at electrical source2",
        "Mode": "Isolate"
    }
}, {
    "0": {
        "ChkValue": "false",
        "Description": "Wear ear-muffs when using machine",
        "Mode": "Isolate"
    }
}]
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • possible duplicate of [Can I get a javascript object property name that starts with a number?](http://stackoverflow.com/questions/5809790/can-i-get-a-javascript-object-property-name-that-starts-with-a-number) – Quentin Jan 10 '14 at 07:04
  • 1
    Did you try using JSON parsing functions? What happened when you did? where exactly did you get stuck? – Floris Jan 10 '14 at 07:05

4 Answers4

3

This is a basic javascript object traversal problem. To access the data inside the second object (that says "Made sure guards are in place..."), you would do:

jsonObj[1]["0"].Description
C Fairweather
  • 696
  • 4
  • 20
0

You can use the JSON.parse() function to work with it in JS.

sachleen
  • 30,730
  • 8
  • 78
  • 73
0

user JSON.parse() to iterate over JSON

FIDDLE

var a = '[{"ChkValue":"ChkValue","Description":"Description","Mode":"Mode"},{"0":{"ChkValue":"false","Description":"Made sure guards are in place on machine","Mode":"Eliminate"}},{"0":{"ChkValue":"false","Description":"Use Liveguard at electrical source2","Mode":"Isolate"}},{"0":{"ChkValue":"false","Description":"Wear ear-muffs when using machine","Mode":"Isolate"}}]';
var b = JSON.parse(a);
for(var i = 0; i < b.length; i++) {
    if(typeof b[i]["0"] != "undefined") {
        console.log(b[i]["0"].ChkValue);
        console.log(b[i]["0"].Description);
        console.log(b[i]["0"].Mode);
    }
}
yashhy
  • 2,856
  • 5
  • 31
  • 57
  • I need to get it server side – user3003670 Jan 10 '14 at 07:19
  • to get this data to the server side you need AJAX https://developer.mozilla.org/en/docs/AJAX and your question was only about retrieving the data, that too tagged with `javascript` – yashhy Jan 10 '14 at 07:26
0

Use list[index][0]

var list = [
  {
      "ChkValue": "ChkValue",
      "Description":"Description",
      "Mode":"Mode"
  },
  {
      "0": {
          "ChkValue":"false",
          "Description":"Made sure guards are in place on machine",
          "Mode":"Eliminate"
      }
  }, 
  {
      "0": {
          "ChkValue":"false",
          "Description":"Use Liveguard at electrical source2",
          "Mode":"Isolate"
      }
  },
  {
      "0": {
          "ChkValue":"false","Description":"Wear ear-muffs when using machine",
          "Mode":"Isolate"
      }
  }
];

console.log(list[1][0].ChkValue); // get "false"
ucdream
  • 691
  • 1
  • 7
  • 18