如何在javascript中解析JSON文件而不了解文件内容? [英] How to parse a JSON file in javascript without having any knowledge about the contents of the file?

查看:75
本文介绍了如何在javascript中解析JSON文件而不了解文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何解析任何JSON文件而不管内容如何。
例如,我必须解析以下JSON。如何在不使用JSON文件中的单词/键的情况下解析它并调用值?

I would like to know how to parse any JSON file irrespective of the contents. For example, I have to parse the following JSON. How would I parse it and call values without using words/keys from the JSON file?

var a = [{
    "Master Row": "P558:15",
    "Prefix*": "5C34",
    "Base*": "1508",
    "Suffix*": "CA",
    "Weight Unit of Measure": "lb"
}, {
    "Master Row": "P558:16",
    "Prefix*": "5C34",
    "Base*": "1508",
    "Suffix*": "CA",
    "Weight Unit of Measure": "lb"
}]


推荐答案

你需要一个循环遍历a中的每个元素,以及循环遍历对象中的每个键。
(另外你在第一个对象中缺少前缀和基数之间的逗号)

You will need a loop to go through each element in a, and a loop to go through each key in the objects. (Also you are missing a comma between prefix and base in the first object)

var a= [ { "Master Row":"P558:15", "Prefix*":"5C34","Base*":"1508", "Suffix*":"CA", "Weight Unit of Measure":"lb" }, { "Master Row":"P558:16", "Prefix*":"5C34", "Base*":"1508", "Suffix*":"CA", "Weight Unit of Measure":"lb" } ];

for(i=0;i<a.length;i++){
  var temp=a[i];
  for(key in temp){
    console.log(key+' '+temp[key]);
  }
}

这篇关于如何在javascript中解析JSON文件而不了解文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆