如何在android中创建嵌套的动态Json Object pojo类

时间:2016-05-03 07:21:30

标签: android json retrofit pojo

我有以下json格式: -

    {"status": "ok","response": {
    "results": {
      "C9DC8215200A9180BD4F4F15097CEB0AC6F4DB6C57FDB75F": {
        "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F": {
          "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "0D9F4046200A8059BFB6FF7AF0BA54045D73A13C70E14D7A",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "7CFD035E200A4641A670DF08EAF9870A03986F7C350172FA",
                "comment": null,
                "created_on": "1455099295",
                "applied_on": "1456099199",
                "created_by": "D185D3E3200058716235DFECB7C58C0604923B9CEBE14382",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "7CFD035E200A4641A670DF08EAF9870A03986F7C350172FA",
                "name": "Got it",
                "value": "1.00",
                "colour": "007878",
                "is_editable": true,
                "created_on": "1454341373",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "3",
                "is_target": false,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EAF8D3DA200A8F7E32C81FB1ACA9920BC26B9E8C40433A23",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454500600",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EAEDD3CF200A8B8BABB87FBEDC379A060622053CEB33CAD2",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454500589",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "1D781304200A82AEE6112F19456F6F0C8412131C97CF0226",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "comment": null,
                "created_on": "1454054776",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "name": "Problem",
                "value": "0.00",
                "colour": "ff0000",
                "is_editable": true,
                "created_on": "1453107752",
                "created_by": null,
                "display_order": "1",
                "is_target": false,
                "has_events": null
              }
            }
          ],
          "ED4FE4222008FE269BEDDFDBC1ED2D0CA985303C2E22EA80": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EB1C10DC200A8544DB986F4C9ECCDA0A959D13DCCBFE4CE2",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE4222008FE269BEDDFDBC1ED2D0CA985303C2E22EA80",
                "item_uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "comment": null,
                "created_on": "1454500636",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "name": "Problem",
                "value": "0.00",
                "colour": "ff0000",
                "is_editable": true,
                "created_on": "1453107752",
                "created_by": null,
                "display_order": "1",
                "is_target": false,
                "has_events": null
              }
            }
          ],
          "ED4FE36D2008F0BA0E740F8A4733B80CF55C145C5B8A417B": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "93C0D9D5200A88889B023F814473CD016DEB0EFCC7094543",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE36D2008F0BA0E740F8A4733B80CF55C145C5B8A417B",
                "item_uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "comment": null,
                "created_on": "1454478272",
                "applied_on": "1456099199",
                "created_by": "D185D3E3200058716235DFECB7C58C0604923B9CEBE14382",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "name": "Problem",
                "value": "0.00",
                "colour": "ff0000",
                "is_editable": true,
                "created_on": "1453107752",
                "created_by": null,
                "display_order": "1",
                "is_target": false,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "1D7C4909200A86C9E7641FFDD9CA1F0FA0A4B73CA232DED1",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE36D2008F0BA0E740F8A4733B80CF55C145C5B8A417B",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454054780",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            }
          ]
        },
        "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC": {
          "ED4FE4222008FE269BEDDFDBC1ED2D0CA985303C2E22EA80": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EB487A39200A80C54701FFA72E4CF10362D20BDC75603A6F",
                "user_uuid": "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC",
                "learning_objective_uuid": "ED4FE4222008FE269BEDDFDBC1ED2D0CA985303C2E22EA80",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454500680",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            }
          ],
          "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EAF8D47D200A873635717F745065A1096F5D638C8CBBF34A",
                "user_uuid": "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454500600",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "EAEDD466200A8E8AB7676F73A874CE079D5F6D7C885CFEEB",
                "user_uuid": "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454500589",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "1D7813A2200A80CD64CBDF6782AC3B050F01DFFCE04B6D11",
                "user_uuid": "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "comment": null,
                "created_on": "1454054776",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "name": "Problem",
                "value": "0.00",
                "colour": "ff0000",
                "is_editable": true,
                "created_on": "1453107752",
                "created_by": null,
                "display_order": "1",
                "is_target": false,
                "has_events": null
              }
            }
          ],
          "ED4FE36D2008F0BA0E740F8A4733B80CF55C145C5B8A417B": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "1D7C49A3200A86022A217F06045A8C0F8E19871CA03CA051",
                "user_uuid": "EDF8AB8320005CF46FD85FB2057DC1040B6045FC145037CC",
                "learning_objective_uuid": "ED4FE36D2008F0BA0E740F8A4733B80CF55C145C5B8A417B",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1454054780",
                "applied_on": "1456099199",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            }
          ]
        }

      },
      "C9DC824B200A9C418704CF994FD5F40662FC79ECEFA00551": {
        "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F": {
          "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2": [
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "A8A868D9200A8830B097EF898409A40D9ADD24EC9988F744",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "comment": null,
                "created_on": "1458284712",
                "applied_on": "1460329199",
                "created_by": "D185D3E3200058716235DFECB7C58C0604923B9CEBE14382",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "54BD732E200A4DD528CD9F420735D60D9B9CE18C411E0E51",
                "name": "Problem",
                "value": "0.00",
                "colour": "ff0000",
                "is_editable": true,
                "created_on": "1453107752",
                "created_by": null,
                "display_order": "1",
                "is_target": false,
                "has_events": null
              }
            },
            {
              "type": "student_tracker_status_type",
              "event": {
                "uuid": "9477559E200A84B71BA45F446D73CD053F7234DC097E5D47",
                "user_uuid": "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
                "learning_objective_uuid": "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2",
                "item_uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "comment": null,
                "created_on": "1458279543",
                "applied_on": "1460329199",
                "created_by": "D185D3E3200058716235DFECB7C58C0604923B9CEBE14382",
                "updated_on": null,
                "updated_by": null
              },
              "item": {
                "uuid": "1B9BCDD0200A479A2870AF4FA9B80C0583FF0C8C740733D4",
                "name": "All Good",
                "value": "1.00",
                "colour": "5bce40",
                "is_editable": true,
                "created_on": "1454054299",
                "created_by": "AEBB466920005BA2486F0FC9F9AAC10FDF77900C90146CF6",
                "display_order": "2",
                "is_target": true,
                "has_events": null
              }
            }
          ]
        }
      }
    }
  }

}

在上面的Json中,我有几个动态Json对象键,它们会针对不同的响应进行更改。我正在使用改造来解析json。请帮助我如何为POJO以上的JSON课程创建。 谢谢!

1 个答案:

答案 0 :(得分:2)

通常像Gson这样的JSON解析器可以解析JSON对象中的动态键,将对象转换为Java Map。

在你的情况下,JSON对象非常花哨,产生的POJO可能是这样的:

private class Wrapper {
    String status;
    Response response;
}

private class Response {
    Map<String, Map<String, Map<String, List<Inner>>>> results;
}

private class Inner {
    String type;
    ... all other stuff
}

但是这个:Map<String, Map<String, Map<String, List<Inner>>>>对我来说看起来不太好。

尝试用Gson认为它似乎工作正常:

Wrapper wrapper = new Gson().fromJson(json, Wrapper.class);

    Log.e(TAG, "Type: " + wrapper.response.results
        .get("C9DC8215200A9180BD4F4F15097CEB0AC6F4DB6C57FDB75F")
        .get("EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F")
        .get("ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2").get(0).type);

记录MainActivity: Type: student_tracker_status_type

希望这有帮助。