猿问

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

我有 JSON 数据,想仅提取 Java 中特定字段的数据并将其存储在 String 中。示例,

来自问题,键:651,来自项目名称:测试,更新,创建 数组问题的所有记录的此详细信息。


示例 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": "",

                }

            }

            }

    ]


慕仙森
浏览 282回答 2
2回答

30秒到达战场

获取值时可以转换为字符串。像这样更改您的代码,看看它是否有帮助。JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray issues = (JsonArray) object.get("issues");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject issues0 = (JsonObject) issues.get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String issue_key = (String) issues0.get("key");//<---here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value of key is -> " + issue_key);&nbsp;更新如果您想要所有值,只需将其放在“for”中:JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray issues = (JsonArray) object.get("issues");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<issues.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject issue = (JsonObject) issues.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String issue_key = (String) issue.get("key");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }更新 2“更新”和“创建”的数据不在“问题”内,它们在“字段”内才能访问它们,您需要从“字段”中获取它们。您必须逐级进入内部才能访问变量:JsonObject object = (JsonObject) new JsonParser().parse(new FileReader("C:\\MyData\\response.json"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray issues = (JsonArray) object.get("issues");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i<issues.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject issue = (JsonObject) issues.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String issue_key = (String) issue.get("key");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject fields = (JsonObject) issues.get("fields");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject project = (JsonObject) issues.get("project");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String project_name = (String) project.get("key");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fields_updated = (String) fields.get("updated");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fields_created = (String) fields.get("created");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Value of key" + Integer.toString(i + 1) + " is -> " + issue_key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

婷婷同学_

您收到一个错误,因为您将 a 投射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.
随时随地看视频慕课网APP

相关分类

Java
我要回答