如何使用 GSON 解析这种类型的 json?

How to parse this type of json using GSON?

需要解析JSON到Java对象,下面代码中如何使用逗号分隔的字符串作为对象示例Intrest来创建列表。

{
  "following": [
    
  ],
  "website": "",
  "name": "Bala",
  "gender": "",
  "interest": [
    "Cricket",
    "FootBall",
    "BasketBall"
  ],
  "isConfirmed": true,
  "posturl": [
    "https://www.google.com",
    "https://www.google.com"
  ],
  "age": ""
}
json 中的

interest 不是逗号分隔的字符串。它是一个数组。您可以将其映射到 List<String> 输入 java 对象。

Gson gson = new GsonBuilder().create();
JsonObject jsonObject = gson.fromJson(String YourJSONObject, JsonObject.class);

## Parsing JSON object ##
JsonArray interestarray = jsonObject.getAsJsonArray("interest");


## Storing JSONArray elements into Arraylist ##
List<String> list=new ArrayList<>();
for(JsonElement interest:interestarray){
list.add(interest.getAsString());
}