将复杂的JSON对象解析为Java对象并添加到recyclerview

时间:2019-02-06 11:39:47

标签: android json gson

onResponse(JSONObject response)我想从服务器获取数据并将其解析到回收者视图。我在stackoverflow上看到了很多答案,但是找不到用于我的复杂JSON数据的任何合适的解决方案。我正在使用排球库,也许GSON是映射Java对象的正确方法。请告诉我从此json获取响应的正确方法。我只想使用“标题”,“正文”和“ createdAt”的信息。以下是我在授权令牌的帮助下收到的json的一部分。

{
"content": [
    {
        "id": "5c56e41245dfac0001100221",
        "createdAt": "2019-02-03T12:52:34.225+0000",
        "productName": "ams",
        "title": "WPB Plan hass been APPROVED",
        "body": "Dear Operator , WPB Plan Report Report has been Checked and Acknowledged.",
        "data": {
            "subModule": "wpb",
            "moduleName": "planning",
            "workspaceName": "Planing module ",
            "category": "wpb",
            "url": "http://pulse.target.meeraspace.com/ams/planning/wpb/details/5c56df5e2812a00001a23dc3",
            "taskId": "182951",
            "workspaceId": "360"
        },
        "from": {
            "sub": "CiRlMTNjNDgxZC1iZDcwLTU0OTMtOWI1NS1kNjVhY2Q3OTM2MWQSBWxvY2Fs",
            "name": "hashim",
            "email": "hashim.shamsi@targetofs.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c56bba345dfac00011001fa",
        "createdAt": "2019-02-03T10:00:03.314+0000",
        "productName": "ams",
        "title": "WPB Plan hass been APPROVED",
        "body": "Dear Operator , WPB Plan Report Report has been Checked and Acknowledged.",
        "data": {
            "subModule": "wpb",
            "moduleName": "planning",
            "workspaceName": "Work Space Testing on EDGE Jan 2019",
            "category": "wpb",
            "url": "http://pulse.target.meeraspace.com/ams/planning/wpb/details/5c56b9182812a00001a23db9",
            "taskId": "182856",
            "workspaceId": "31"
        },
        "from": {
            "sub": "CiRkODAyNWJhNy01ODJlLTU0NzktOTE4MS01MGE5ZTZmZGY3YWYSBWxvY2Fs",
            "name": "Rajesh M",
            "email": "rajesh.mhamunkar@targetofs.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c53e9f545dfac000110019f",
        "createdAt": "2019-02-01T06:40:53.558+0000",
        "productName": null,
        "title": "Workspace",
        "body": "invites you to workspace CSR",
        "data": {
            "workspaceId": "308",
            "workspaceName": "CSR",
            "moduleName": "workspace",
            "subModuleName": "base",
            "description": "Workspace invite",
            "url": "/workspace/details/308"
        },
        "from": {
            "sub": "CiQ2MjIxY2FjZS05OGM4LTU5NWEtOTc2NS1kYTNiN2QyODM0ZTkSBWxvY2Fs",
            "name": "Usman Aleem",
            "email": "usman.aleem@targetofs.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c52c67945dfac0001100148",
        "createdAt": "2019-01-31T09:57:13.404+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "Jane",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c52c67945dfac0001100147",
            "workspaceId": "247"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c52c42945dfac0001100144",
        "createdAt": "2019-01-31T09:47:21.425+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "Jane",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c52c42945dfac0001100143",
            "workspaceId": "247"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c52c41e45dfac0001100140",
        "createdAt": "2019-01-31T09:47:10.714+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "Jane",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c52c41e45dfac000110013f",
            "workspaceId": "247"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4ff2eed669df00018df86d",
        "createdAt": "2019-01-30T11:40:36.997+0000",
        "productName": "workspace-message-345",
        "title": "you have 5 new messages",
        "body": "you have 5 new messages",
        "data": {
            "totalNotification": 5,
            "moduleName": "message",
            "workspaceName": "Testssfsdf",
            "message": [
                {
                    "channelId": "5c4fdcb2d669df00018df85d",
                    "type": "direct",
                    "unreadMessages": 4
                },
                {
                    "channelId": "5c500a58a20c78000129aaff",
                    "type": "group",
                    "unreadMessages": 1
                }
            ],
            "workspaceId": "345"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c510af6a20c78000129ab8c",
        "createdAt": "2019-01-30T02:24:54.276+0000",
        "productName": "workspace-message-351",
        "title": "you have 1 new message",
        "body": "you have 1 new message",
        "data": {
            "totalNotification": 1,
            "moduleName": "message",
            "workspaceName": "test-by-benjamin",
            "message": [
                {
                    "channelId": "5c4fdcb2d669df00018df85d",
                    "type": "direct",
                    "unreadMessages": 1
                }
            ],
            "workspaceId": "351"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4fdadad669df00018df858",
        "createdAt": "2019-01-29T04:47:22.035+0000",
        "productName": null,
        "title": "Workspace",
        "body": "invites you to workspace Testssfsdf",
        "data": {
            "workspaceId": "345",
            "workspaceName": "Testssfsdf",
            "moduleName": "workspace",
            "subModuleName": "base",
            "description": "Workspace invite",
            "url": "/workspace/details/345"
        },
        "from": {
            "sub": "CiRhYjMyYzZmMS1hYjBhLTVhZjctOTFjYy04ODg1MDU3MTdkOGISBWxvY2Fs",
            "name": "Yuchao Lu",
            "email": "yuchao.lu@target-energysolutions.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c4efdf3d669df00018df845",
        "createdAt": "2019-01-28T13:04:51.693+0000",
        "productName": "ams",
        "title": "Annual Base Report",
        "body": "Dear Operator ,  Annual Base report for Inventory has been checked and acknowledged.",
        "data": {
            "subModule": "base",
            "moduleName": "inventory",
            "category": "AnnualInventoryProcess",
            "url": "http://pulse.target.meeraspace.com/ams/inventory/base/details/5c4efdcb71306a0001d55c85",
            "taskId": "182281"
        },
        "from": {
            "sub": "CglyZWd1bGF0b3ISBWxvY2Fs",
            "name": "Regulator",
            "email": "regulator@yopmail.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c4efd3bd669df00018df83d",
        "createdAt": "2019-01-28T13:01:47.168+0000",
        "productName": null,
        "title": "Task Assignment",
        "body": "body",
        "data": {
            "moduleName": "taskManager",
            "description": "DESC",
            "url": "/workspace/task-manager/126",
            "taskId": "649"
        },
        "from": {
            "sub": "CiQxODFkYjU5ZS0wZGNjLTVkYmMtOWYxNi1lMjlkMjRiYTNjZTISBWxvY2Fs",
            "name": "Mohsin",
            "email": "targetmohsin@gmail.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c4ef924d669df00018df833",
        "createdAt": "2019-01-28T12:44:20.780+0000",
        "productName": null,
        "title": "Task Assignment",
        "body": "body",
        "data": {
            "moduleName": "taskManager",
            "description": "DESC",
            "url": "/workspace/task-manager/126",
            "taskId": "621"
        },
        "from": {
            "sub": "CiQxODFkYjU5ZS0wZGNjLTVkYmMtOWYxNi1lMjlkMjRiYTNjZTISBWxvY2Fs",
            "name": "Mohsin",
            "email": "targetmohsin@gmail.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c49dc92d669df00018df73c",
        "createdAt": "2019-01-24T15:41:06.964+0000",
        "productName": "ams",
        "title": "WPB Plan",
        "body": "Dear TECOM members, WPB Plan Report has been submitted, please check and acknowledge.",
        "data": {
            "subModule": "wpb",
            "moduleName": "planning",
            "workspaceName": "test 1",
            "category": "wpb",
            "url": "http://pulse.target.meeraspace.com/ams/planning/wpb/details/5c49dc9071306a0001d55c44",
            "taskId": "180930",
            "workspaceId": "317"
        },
        "from": {
            "sub": "CghvcGVyYXRvchIFbG9jYWw",
            "name": "Operator",
            "email": "operator@meerawork.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c49dc91d669df00018df739",
        "createdAt": "2019-01-24T15:41:05.757+0000",
        "productName": "ams",
        "title": "WPB Plan",
        "body": "Dear TECOM members, WPB Plan Report has been submitted, please check and acknowledge.",
        "data": {
            "subModule": "wpb",
            "moduleName": "planning",
            "workspaceName": "test 1",
            "category": "wpb",
            "url": "http://pulse.target.meeraspace.com/ams/planning/wpb/details/5c49dc9071306a0001d55c44",
            "taskId": "180923",
            "workspaceId": "317"
        },
        "from": {
            "sub": "CghvcGVyYXRvchIFbG9jYWw",
            "name": "Operator",
            "email": "operator@meerawork.com",
            "valid": true
        },
        "viewed": false
    },
    {
        "id": "5c495629d669df00018df715",
        "createdAt": "2019-01-24T06:07:37.072+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "Android",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c495629d669df00018df714",
            "workspaceId": "126"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4635d6d669df00018df6ad",
        "createdAt": "2019-01-21T21:12:54.801+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4635d6d669df00018df6ac",
            "workspaceId": "314"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4635b5d669df00018df6aa",
        "createdAt": "2019-01-21T21:12:21.382+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4635b5d669df00018df6a8",
            "workspaceId": "314"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c46357ed669df00018df6a6",
        "createdAt": "2019-01-21T21:11:26.883+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c46357ed669df00018df6a4",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c463538d669df00018df6a0",
        "createdAt": "2019-01-21T21:10:16.935+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c463537d669df00018df69e",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4634ebd669df00018df69c",
        "createdAt": "2019-01-21T21:08:59.607+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4634ebd669df00018df69a",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4632abd669df00018df68d",
        "createdAt": "2019-01-21T20:59:23.197+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4632abd669df00018df68c",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c46327ad669df00018df689",
        "createdAt": "2019-01-21T20:58:34.525+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c46327ad669df00018df688",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c46322cd669df00018df685",
        "createdAt": "2019-01-21T20:57:16.298+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c46322cd669df00018df684",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4631c0d669df00018df67e",
        "createdAt": "2019-01-21T20:55:28.482+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4631c0d669df00018df67d",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c462ed4d669df00018df674",
        "createdAt": "2019-01-21T20:43:00.419+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c462ed4d669df00018df673",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c462ac0d669df00018df670",
        "createdAt": "2019-01-21T20:25:36.835+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c462ac0d669df00018df66f",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c462a87d669df00018df66d",
        "createdAt": "2019-01-21T20:24:39.704+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c462a87d669df00018df66b",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c46294dd669df00018df668",
        "createdAt": "2019-01-21T20:19:25.889+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c46294dd669df00018df667",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4624bbd669df00018df664",
        "createdAt": "2019-01-21T19:59:55.793+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4624bbd669df00018df663",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4623d3d669df00018df660",
        "createdAt": "2019-01-21T19:56:03.623+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4623d3d669df00018df65f",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c46234bd669df00018df65c",
        "createdAt": "2019-01-21T19:53:47.284+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c46234bd669df00018df65b",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c462296d669df00018df655",
        "createdAt": "2019-01-21T19:50:46.325+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c462296d669df00018df654",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c462234d669df00018df652",
        "createdAt": "2019-01-21T19:49:08.522+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c462234d669df00018df650",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4621cbd669df00018df646",
        "createdAt": "2019-01-21T19:47:23.114+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "First-workspace",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4621cbd669df00018df645",
            "workspaceId": "313"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4583139b09c60001a94445",
        "createdAt": "2019-01-21T08:30:11.922+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "test 1",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4583139b09c60001a94443",
            "workspaceId": "317"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c4581679b09c60001a94440",
        "createdAt": "2019-01-21T08:23:03.482+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "test 1",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c4581679b09c60001a9443f",
            "workspaceId": "317"
        },
        "from": null,
        "viewed": false
    },
    {
        "id": "5c457ff09b09c60001a9443a",
        "createdAt": "2019-01-21T08:16:48.297+0000",
        "productName": "meetings",
        "title": "Meeting invitation",
        "body": "New meeting invitation",
        "data": {
            "moduleName": "meeting",
            "action": "popup",
            "workspaceName": "test 1",
            "category": "meeting invitation",
            "subModuleName": "invitation",
            "url": "/mp/api/v2/meetings/invitation/5c457ff09b09c60001a94439",
            "workspaceId": "317"
        },
        "from": null,
        "viewed": false
    },

3 个答案:

答案 0 :(得分:0)

创建一个或多个模型类(针对您的情况),并在Volley onResponse方法中使用Gson对其进行适当解析。

您可以使用link

在线将JSON响应转换为Java POJO类。

答案 1 :(得分:0)

您可以执行以下操作。在您的onResponse方法中,您收到JSONObject响应。所以像这样将对象添加到模型类中。

Gson gson = new Gson(); 
YourPOJOClass dataResponse = gson.fromJson(response.toString(), YourPOJOClass.class);

我假设您已经成功生成了POJO类。

答案 2 :(得分:0)

您可以尝试使用此在线链接来转换您的json to poja class。您需要做的就是创建一个嵌套的pojo类,并尝试 retrofit 而不是凌空抽射,它已经过时了

我这样使用

String temp = jsonString;
Gson gson = new Gson();            
Favourites favourites = gson.fromJson(temp, Favourites.class);

我的最爱是我的波霍大师班