如何在Java中提取JSON数组的特定字段并将其存储在String中

时间:2019-03-11 05:14:38

标签: java json

我有JSON数据,并且想提取Java中仅特定字段的数据并将其存储在String中。 例子,
   从问题,密钥:651,从项目名称:test,更新已创建数组问题。

示例JSON数据:

"issues": [
        {
           "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
           "key": "651",
           "fields": {
                "project": {
                    "name": "test",
                    "Urls": {
                    "48x48": "https://test1.com",
                        "24x24": "https://test2.com"
                        },
                        },
                        "updated": "2019-03-05T13:24:56.000-0800",
                "created": "2019-03-05T13:24:56.000-0800",
                "status": {
                "description" : "";
                "name": "",
                }

            }
        },
                {
           "expand": "operations,versionedRepresentations,editmeta,changelog,renderedFields",
           "key": "321",
           "fields": {
                "project": {
                    "name": "test2",
                    "Urls": {
                    "48x48": "https://test1.com",
                    "24x24": "https://test2.com"
                    },
                },
                        "updated": "2019-03-05T13:24:56.000-0800",
                "created": "2019-03-05T13:24:56.000-0800",
                "status": {
                "description" : "";
                "name": "",
                }
            }
            }
    ]

到目前为止,我已经尝试过使用jar的Java代码-(gson-2.8.5)

JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
                JsonArray issues = (JsonArray) object.get("issues");
                JsonObject issues0 = (JsonObject) issues.get(0);
                JsonObject issues0data = (JsonObject) issues0.get("key");
                String issue_key = issues0data.get("issue_key").getAsString();
                System.out.println("Value of key is -> " + issue_key);  // java.lang.ClassCastException: com.google.gson.JsonPrimitive cannot be cast to com.google.gson.JsonObject

更新代码

JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));               
                JsonArray issues_data = (JsonArray) object.get("issues");



                for(int i=0; i<issues_data.size(); i++)  
                {
                JsonObject issues = (JsonObject) issues_data.get(i); 
                String issues_key = (String) issues.get("key").toString();
                String project_name = (String) issues.get("name").toString();  // returns null 

               }

2 个答案:

答案 0 :(得分:1)

获取值时可以转换为字符串。 像这样更改代码,看看是否有帮助。

JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
                JsonArray issues = (JsonArray) object.get("issues");
                JsonObject issues0 = (JsonObject) issues.get(0);
                String issue_key = (String) issues0.get("key");//<---here
                System.out.println("Value of key is -> " + issue_key); 

更新

如果要所有值,只需将其放在“ for”内即可:

JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
                JsonArray issues = (JsonArray) object.get("issues");
                for(int i=0; i<issues.size(); i++){
                    JsonObject issue = (JsonObject) issues.get(i);
                    String issue_key = (String) issue.get("key");
                    System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
                }

更新2

“更新”和“创建”的数据不在“问题”中,而是在“字段”中,以便访问它们,您需要从“字段”中获取它们。您必须逐级进入内部才能访问变量:

JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));
                JsonArray issues = (JsonArray) object.get("issues");
                for(int i=0; i<issues.size(); i++){
                    JsonObject issue = (JsonObject) issues.get(i);
                    String issue_key = (String) issue.get("key");
                    JsonObject fields = (JsonObject) issues.get("fields");
                    JsonObject project = (JsonObject) issues.get("project");
                    String project_name = (String) project.get("key");
                    String fields_updated = (String) fields.get("updated");
                    String fields_created = (String) fields.get("created");
                    System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);
                }

答案 1 :(得分:1)

由于将JsonPrimitive投射到JsonObject上而导致错误。因此,不要使用

JsonObject issues0data = (JsonObject) issues0.get("key");

您应该

String issues0data = issues0.get("key").getAsString();
System.out.println("Value of key is -> " + issues0data);

在这里,调用getAsString()将调用JsonPrimitive.getAsString()方法。如果原语是boolean/number/string并将其转换为string,这将很重要。