如何获取内部JSON的名称

时间:2016-05-18 23:24:12

标签: javascript html json asp-classic

我从记录器上的数据库中获取一些信息,将其修改为JSON表示法。

但现在,我不知道如何只阅读我的专栏名称。

见这个例子:

http://www.jsoneditoronline.org/?id=1bccb6d9522392f4763d58e24ff4a8e6

我需要读取GND_AREA,DCR_AREA名称。

修改

有些人在下面给出了一些有效的答案,但我认为我没有正确解释我的想法。

我的对象有一个名为" List"的密钥,因为JSON具有这种结构:

JSON

我想检索字符串名称。不是价值......

例子:我想要下面的那些字符串,因为这些字符串是我的列名,它们可以根据表格进行更改我搜索我的数据。

GND_AREA,DCR_AREA,DCR_EQUIP等......

P.S:这里的javascript真的很新

结束编辑

我尝试了我在这里找到的所有可能性,但我不知道我的JSON符号是否正确....

我尝试使用java脚本执行此操作。

我试过的最后一个就是这个:

Object.keys(obj).forEach(function (key) {
               console.log (obj[key].name)
            });

这是我的字符串:

{
  "List": [
    {
      "GND_AREA": "Redução",
      "DCR_AREA": "Alto Forno 1",
      "DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28",
      "DCR_TECNCA": "Monitoramento",
      "DCR_DCPLNA": "Temperatura",
      "COD_POSIC": "61019",
      "DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05",
      "DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1",
      "DCR_PNTINS": "Algaraviz 1",
      "DAT_INSP": "23/2/2016 21:28:48",
      "VLR_DP_DESVIO": "",
      "VLR_LTURA": "450",
      "VLR_DMA_DESVIO": "350",
      "COD_UNDMED": "°C "
    },
    {
      "GND_AREA": "Redução",
      "DCR_AREA": "Alto Forno 1",
      "DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28",
      "DCR_TECNCA": "Monitoramento",
      "DCR_DCPLNA": "Temperatura",
      "COD_POSIC": "61019",
      "DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05",
      "DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1",
      "DCR_PNTINS": "Algaraviz 1",
      "DAT_INSP": "24/2/2016 21:37:49",
      "VLR_DP_DESVIO": "",
      "VLR_LTURA": "140",
      "VLR_DMA_DESVIO": "350",
      "COD_UNDMED": "°C "
    }
  ]
}

4 个答案:

答案 0 :(得分:2)

试试这个: Working Example

var t = setToYourString;

t.List.forEach(function(el){
  console.log(el.GND_AREA, el.DCR_AREA);
});

您希望对象上的属性不是值:

t.List.forEach(function(el){
  var obj = el;
  for (var prop in el) {
    console.log(prop);
  }
});

https://jsbin.com/qudeda/6/edit?js,console

在您的代码中,name不在该对象上,您尝试读取对象属性的方式需要一个名为name的键,它不会。但它确实有List

答案 1 :(得分:2)

这可能有助于解释。将这些函数放在代码中并调用init(),它将在控制台中显示JSON的属性。

function display(obj, lvl) {
    var tab = Array(lvl + 1).join("\t");
    for (var property in obj) {
        var type = typeof obj[property];
        if (obj.hasOwnProperty(property)) {
            console.log(tab+"'"+property + "' of type "+type+" and value: "+obj[property]);
        }
        if (type == 'object') {
            console.log(tab+"which has the following properties:");
            display(obj[property], lvl + 1);
        }
    }
}
function init() {
    var json = '{"List":[{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1","DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28","DCR_TECNCA": "Monitoramento","DCR_DCPLNA": "Temperatura","COD_POSIC": "61019","DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05","DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1","DCR_PNTINS": "Algaraviz 1","DAT_INSP": "23/2/2016 21:28:48","VLR_DP_DESVIO": "","VLR_LTURA": "450","VLR_DMA_DESVIO": "350","COD_UNDMED": "°C "},{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1","DCR_EQUIP": "AF1-Conjunto de Sopro 01 a 28","DCR_TECNCA": "Monitoramento","DCR_DCPLNA": "Temperatura","COD_POSIC": "61019","DCR_POSIC": "OB-F1-RE-ANVE-CJSO-01A05","DCR_POSFUN": "Conjunto de Sopro 01-05 do Anel de Vento do Alto Forno 1","DCR_PNTINS": "Algaraviz 1","DAT_INSP": "24/2/2016 21:37:49","VLR_DP_DESVIO": "","VLR_LTURA": "140","VLR_DMA_DESVIO": "350","COD_UNDMED": "°C "}]}';
    var obj = JSON.parse(json);
    console.log("My JSON has properties named:");
    display(obj, 1);
}

P.S。 obj.propertyobj['property']

的语法糖

答案 2 :(得分:0)

假设您将字符串存储在变量jsonString中。然后,您可以解析它并读取所需的属性(这里我将信息存储在两个数组中)。

var jsonString = JSON.parse('{"List": [{"GND_AREA": "Redução","DCR_AREA": "Alto Forno 1"},{"GND_AREA": "Aumento","DCR_AREA": "Alto Forno 2"}]}');

var gndAreas = [];
var dcrAreas = [];

for(var i = 0; i < jsonString.List.length; i++){

   gndAreas.push(jsonString.List[i].GND_AREA);
   dcrAreas .push(jsonString.List[i].DCR_AREA);

}

答案 3 :(得分:0)

感谢@JordanHendrix和@Tibrogargan!

两种方法都有效!我刚修改了一下。

@JordanHendrix提供的方法:

var obj = t.List[0];
for (var prop in obj) {
  console.log(prop);
}

https://jsbin.com/gufubevozo/edit?js,console

@Tibrogargan提供的方法:

function display(obj2, lvl) {
var tab = Array(lvl + 1).join("\t");

var obj = obj2.List[0];

var i = 0;
var loop = 0;

for (var property in obj) {
    var type = typeof obj[property];

    if (obj.hasOwnProperty(property)) {
        console.log(property);                        
    } 
}

}

https://jsbin.com/qomunayiko/edit?js,console

我标记了Tibrogargan的答案,因为它更完整。