如何访问Json Data?

时间:2014-01-10 07:01:31

标签: javascript json

任何人都可以帮我从json获取数据。我已经获得了下面格式的json数据,在这个json中你可以看到每个记录中都有“{0}”。所以我的问题是我怎么样可以从这种格式获取数据,或者有什么方法可以从json中删除“{0}”。

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

4 个答案:

答案 0 :(得分:3)

这是一个基本的javascript对象遍历问题。 要访问第二个对象内部的数据(说明“确保警卫到位......”),您可以这样做:

jsonObj[1]["0"].Description

答案 1 :(得分:0)

您可以使用JSON.parse()函数在JS中使用它。

答案 2 :(得分:0)

用户JSON.parse()迭代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);
    }
}

答案 3 :(得分:0)

使用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"