递归提取JSON密钥标签 - Java

时间:2016-09-27 16:45:24

标签: java json

我很好奇是否有办法在Java中的JSON对象中提取所有关键标签(包括子项)?

例如,假设我有这个JSON对象(单行中的第一个):

.footer-socket-wrapper .copyright {

}

同一个JSON对象的另一个视图:

{"class":{"student":[{"firstname":"dinkar","grade":"A","nickname":["dinkar","dinkar jr"],"rollno":393,"marks":85,"lastname":"kad"},{"cars":{"model":[{"content":"Ford","spec":"RS"},"Toyota","Subaru"]},"firstname":"Vaneet","nickname":"vinni","friend":"Robert","rollno":493,"lastname":"Gupta"},{"firstname":"jasvir","nickname":"jazz","Driving":99,"rollno":593,"marks":90,"lastname":"singn"}]}}

我想获取此对象中的所有关键标记,但我不知道有多少关键标记。所以我所做的就是递归遍历每个元素并将所有唯一的JSON密钥标记放入JSON数组并打印出来。这仅适用于第一层,但如果有多个层我不确定会发生什么?如果您有任何建议,请告诉我,谢谢。

{
  "class": {
    "student": [{
      "firstname": "dinkar",
      "grade": "A",
      "nickname": ["dinkar", "dinkar jr"],
      "rollno": 393,
      "marks": 85,
      "lastname": "kad"
    }, {
      "cars": {
        "model": [{
          "content": "Ford",
          "spec": "RS"
        }, "Toyota", "Subaru"]
      },
      "firstname": "Vaneet",
      "nickname": "vinni",
      "friend": "Robert",
      "rollno": 493,
      "lastname": "Gupta",
      "firstname": "jasvir",
      "nickname": "jazz",
      "Driving": 99,
      "rollno": 593,
      "marks": 90,
      "lastname": "singn"
    }]
  }
}

0 个答案:

没有答案