简体   繁体   English

如何遍历JSON文件?

[英]How to traverse a JSON file?

The JSON I have is mentioned below :- 下面提到了我拥有的JSON:-

{   head: {
    link: [],
    vars: [
      "CompanyName",
      "Company_Name",
      "Foundation_URI",
      "Foundation_Name",
      "Latitude",
      "Longitude"
    ]   },   results: {
    distinct: false,
    ordered: true,
    bindings: [
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/United_Christian_Broadcasters"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "United Christian Broadcasters"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      },
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/United_Christian_Broadcasters"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "UCB Media"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      },
      {
        CompanyName: {
          type: "uri",
          value: "http://dbpedia.org/resource/Kathmandu_%28company%29"
        },
        Company_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Kathmandu"
        },
        Foundation_URI: {
          type: "uri",
          value: "http://dbpedia.org/resource/Christchurch"
        },
        Foundation_Name: {
          type: "literal",
          xml:lang: "en",
          value: "Christchurch"
        },
        Latitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "-43.52999877929688"
        },
        Longitude: {
          type: "typed-literal",
          datatype: "http://www.w3.org/2001/XMLSchema#float",
          value: "172.6202850341797"
        }
      }
    ]   } }

I want to know that how can I traverse this JSON to get the values of the appropriate variables as mentioned in the JSON file. 我想知道如何遍历此JSON以获取JSON文件中提到的适当变量的值。 I would like to know this with respect to JavaScript as well as Java. 我想就JavaScript和Java知道这一点。 Please let me know how to traverse this JSON so as to get data easily. 请让我知道如何遍历此JSON,以便轻松获取数据。

This is not a JSON string but luckily a YAML standrd format. 这不是JSON字符串,但幸运的是YAML标准格式。

You can use YAML library to transverse your jSON-like string 您可以使用YAML库横穿类似jSON的字符串

Here is a rather simple tree traversal routine: 这是一个相当简单的树遍历例程:

function traverse(o) {
  if( o instanceof Object ) {
    for( key in o ) {
      traverse(o[key]);
    }
  }
  else if( o instanceof Array ) {
    for( value in o ) {
      traverse(value);
    }
  }
  else {
    console.log(o);
  }
}
var q = { name : 'me', data : [ { a: 'a1', b: 'b1' }, { a: 'a2', b: 'b2'} ] };
traverse(q);

However, I think this is not quite what you're looking for. 但是,我认为这并不是您想要的。 Please clarify and I will update my answer accordingly. 请澄清,我将相应地更新我的答案。

Following is a simple traversing of JSON object, this should help you undersatnd your JSON object normal traversing. 以下是JSON对象的简单遍历,这应该有助于您了解JSON对象的正常遍历。 Check the https://github.com/substack/js-traverse link for a detailed tutorial for complex traversing of any javascript object 检查https://github.com/substack/js-traverse链接,以获取有关详细遍历任何javascript对象的详细教程

<script language="javascript"> 

var emp = {"details" : [
             {
             "Name" : "Nitin1",
             "Salary" : 10000, 
             "DOJ" : "16th Sept 2010"
             }
            ,

             {"Name" : "Abhijit2", 
             "Salary" : 5000, 
             "DOJ" : "15th Sept 2010"}
            ,

             {"Name" : "Nilesh", 
             "Salary" : 50000, 
             "DOJ" : "10th Sept 2010"}

            ]
    };

document.writeln("<table border='1'><tr><td>Name</td><td>Salary</td><td>DOJ</td></tr>");
var i=0;
for(i=0;i<emp.details.length; i++)
{
  document.writeln("<tr><td>" + emp.details[i].Name + "</td>");
  document.writeln("<td>" + emp.details[i].Salary +"</td>");
  document.writeln("<td>" + emp.details[i].DOJ +"</td></tr>");
}
document.writeln("</table>");


</script>

If you only want to retrieve particular values - you do not need to traverse the entire JSON file. 如果您只想检索特定值,则无需遍历整个JSON文件。

This can be done in Javascript as follows: 这可以用Javascript完成,如下所示:

Declared JSON variable: 声明的JSON变量:

var myJSONObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

Fetch a particular value: 获取特定值:

myJSONObject.bindings[0].method    // "newURI"

For a full reference you can go here . 有关完整参考,您可以在这里

I do not use Java myself, but XML.java (documentation available here ) allows you to convert the JSON into XML. 我自己没有使用Java,但是XML.java(可在此处找到文档)允许您将JSON转换为XML。 Parsing an XML is easy - you will find plenty of tutorials on it for Java such as: 解析XML很容易-您会在Java上找到很多关于它的教程,例如:

http://www.seas.gwu.edu/~simhaweb/java/xml/xml.html http://www.seas.gwu.edu/~simhaweb/java/xml/xml.html

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM