简体   繁体   English

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

[英]How to parse this type of json using GSON?

Need to parse the JSON to Java Object, how to create a list using comma-separated string as an object example Intrest in the code below. Need to parse the JSON to Java Object, how to create a list using comma-separated string as an object example Intrest in the code below.

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

interest in the json is not a comma separated String. interest不是逗号分隔的字符串。 It is an array.它是一个数组。 You can map it to List<String> type in java object.您可以在 map 中List<String>类型的 java object。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM