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

我有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": "",

                }

            }

            }

    ]

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


慕雪6442864
浏览 113回答 2
2回答

慕桂英3389331

获取值时,可以转换为字符串。像这样更改代码,看看是否有帮助。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 强制转换为 。所以而不是使用JsonPrimitiveJsonObjectJsonObject&nbsp;issues0data&nbsp;=&nbsp;(JsonObject)&nbsp;issues0.get("key");你应该做的String&nbsp;issues0data&nbsp;=&nbsp;issues0.get("key").getAsString(); System.out.println("Value&nbsp;of&nbsp;key&nbsp;is&nbsp;->&nbsp;"&nbsp;+&nbsp;issues0data);在这里,调用将调用方法。如果基元是,这将小心,并将其转换为 。getAsString()JsonPrimitive.getAsString()boolean/number/stringstring
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java