如何使用gson将Array的对象转换为单独的数组?

问题描述 投票:0回答:2

我正在收到这样的API响应

{
  "$id": "1",
  "Chapters": [
    {
      "$id": "2",
      "Chapter": 11,
      "ChapterName": "Nuclear Theory ",
    }],
 "Subjects": [
    {
      "$id": "117",
      "SubjectId": 9,
      "SubjectName": "Science"
    }],

"Standards": [
    {
      "$id": "151",
      "StandardMasterId": 17,
      "StandardName": "11TH APPEARING (NEET COMBINED)",
    }]

}

我想将此响应(字符串)转换为类似于ArrayList<Chapters>, ArrayList<Subjects>的分隔符。我为章节,主题和标准设置了单独的课程。如何使用GSON进行此操作。

java android gson
2个回答
1
投票

创建模型类以解析数据

public class ResponseDataModel {

    @SerializedName("$id")
    private string id;

    @SerializedName("Chapters")
    private List<Chapters> Chapters;


    @SerializedName("Subjects")
    private List<Chapters> Chapters;

    @SerializedName("Standards")
    private List<Chapters> Chapters;



    /** Generate Getter setter methods **/

}

在您的主要活动中尝试此

String response = "{
  "$id": "1",
  "Chapters": [
    {
      "$id": "2",
      "Chapter": 11,
      "ChapterName": "Nuclear Theory ",
    }],
 "Subjects": [
    {
      "$id": "117",
      "SubjectId": 9,
      "SubjectName": "Science"
    }],

"Standards": [
    {
      "$id": "151",
      "StandardMasterId": 17,
      "StandardName": "11TH APPEARING (NEET COMBINED)",
    }]

}"

Gson gson = new Gson();
ResponseDataModel model = gson.fromGson(response,ResponseDataModel.class);

ArrayList<Subject> subjects = model.get....

1
投票

使用Gson().fromJson将您的响应转换为模型对象

ApiResponse apiResponse = new Gson().fromJson(your_response, ApiResponse.class);

ApiResponse.java:

public class ApiResponse {

    @SerializedName("$id")
    public String id;
    @SerializedName("Chapters")
    public List<Chapter> chapters = null;
    @SerializedName("Subjects")
    public List<Subject> subjects = null;
    @SerializedName("Standards")
    public List<Standard> standards = null;

    //getter-setter
}

Standard.java:

public class Standard {

    @SerializedName("$id")
    public String id;
    @SerializedName("StandardMasterId")
    public int standardMasterId;
    @SerializedName("StandardName")
    public String standardName;

    //getter-setter
}

Subject.java:

public class Subject {

    @SerializedName("$id")
    public String id;
    @SerializedName("SubjectId")
    public int subjectId;
    @SerializedName("SubjectName")
    public String subjectName;

    //getter-setter
}

Chapter.java:

public class Chapter {

    @SerializedName("$id")
    public String id;
    @SerializedName("Chapter")
    public int chapter;
    @SerializedName("ChapterName")
    public String chapterName;

    //getter-setter
}

此外,您的json无效。有效的json应该是这样的:

{
  "$id": "1",
  "Chapters": [
    {
      "$id": "2",
      "Chapter": 11,
      "ChapterName": "Nuclear Theory "
    }
  ],
  "Subjects": [
    {
      "$id": "117",
      "SubjectId": 9,
      "SubjectName": "Science"
    }
  ],
  "Standards": [
    {
      "$id": "151",
      "StandardMasterId": 17,
      "StandardName": "11TH APPEARING (NEET COMBINED)"
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.