java—如何使用javax.json库迭代更多的jsonobject

5vf7fwbs  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(337)

这是我的第一篇文章,所以谢谢你。
我使用javax.json库来读取一个导入的包含json的file.txt文件。
正文如下:

[{
            "role" : "role1",
            "db" : "db_admin",
            "flag" : false,
            "roles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ],
            "inRoles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ]
    },
    {
            "role" : "role2",
            "db" : "db_admin",
            "flag" : false,
            "roles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ],
            "inRoles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ]
    },
    {
            "role" : "role3",
            "db" : "db_admin",
            "flag" : false,
            "roles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ],
            "inRoles" : [
                    {
                            "role" : "role",
                            "db" : "db_admin"
                    }
            ]
    }]

现在,我做了一些尝试,但我只能读第一个对象。这是我阅读第一个jsonarray的代码:

//I read all line and put them into StringBuilder and then in a unique String
String json = sbJson.toString();
  JsonReader jsonreader = Json.createReader(new StringReader(json));
  // Get the JsonObject structure from JsonReader.
  JsonObject jsonobject = jsonreader.readObject();
  jsonreader.close();

  // read json array
  JsonArray jsonarray = jsonobject.getJsonArray("roles");
  JsonObject value = jsonarray.getJsonObject(0);
  String roles_role = value.getString("role");
  String roles_db = value.getString("db");

  jsonarray = jsonobject.getJsonArray("inRoles");
  value = jsonarray.getJsonObject(0);
  String inheritedRoles_role = value.getString("role");
  String inheritedRoles_db = value.getString("db");

所以我可以从第一个jsonobject得到所有的元素,但是我需要其他的元素,我尝试了更多的解决方案,但是它们不适合我。。。

wswtfjt7

wswtfjt71#

你需要做一个for循环来读取所有的内容。你可以这样做

static String json = "[{\n"
            + "            \"role\" : \"role1\",\n"
            + "            \"db\" : \"db_admin\",\n"
            + "            \"flag\" : false,\n"
            + "            \"roles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ],\n"
            + "            \"inRoles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ]\n"
            + "    },\n"
            + "    {\n"
            + "            \"role\" : \"role2\",\n"
            + "            \"db\" : \"db_admin\",\n"
            + "            \"flag\" : false,\n"
            + "            \"roles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ],\n"
            + "            \"inRoles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ]\n"
            + "    },\n"
            + "    {\n"
            + "            \"role\" : \"role3\",\n"
            + "            \"db\" : \"db_admin\",\n"
            + "            \"flag\" : false,\n"
            + "            \"roles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ],\n"
            + "            \"inRoles\" : [\n"
            + "                    {\n"
            + "                            \"role\" : \"role\",\n"
            + "                            \"db\" : \"db_admin\"\n"
            + "                    }\n"
            + "            ]\n"
            + "    }]";

    public static void main(String[] args) throws Exception {
        JsonReader jsonreader = Json.createReader(new StringReader(json));
        // Get the JsonObject structure from JsonReader.
        JsonArray jsonArray = jsonreader.readArray();
        jsonreader.close();

        for(Object j :jsonArray.toArray()){
            System.out.println("j:"+ j);
        }
    }

这就是我在当地的生活

--- exec-maven-plugin:1.5.0:exec (default-cli) @ hackerrank ---
j:{"role":"role1","db":"db_admin","flag":false,"roles":[{"role":"role","db":"db_admin"}],"inRoles":[{"role":"role","db":"db_admin"}]}
j:{"role":"role2","db":"db_admin","flag":false,"roles":[{"role":"role","db":"db_admin"}],"inRoles":[{"role":"role","db":"db_admin"}]}
j:{"role":"role3","db":"db_admin","flag":false,"roles":[{"role":"role","db":"db_admin"}],"inRoles":[{"role":"role","db":"db_admin"}]}
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  0.979 s

在我使用javaxjson的情况下,我有一个mvn项目,并使用了下面的依赖项

<dependency>
  <groupId>javax.json</groupId>
  <artifactId>javax.json-api</artifactId>
  <version>1.1.4</version>
  <type>jar</type>
</dependency>
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.1.4</version>
</dependency>

另外,如果只想从数组中获取一个对象,可以选中https://stackoverflow.com/a/8481439/175554

相关问题