Gson convert JSON to list object
Gson - convert from Json to a typed ArrayListYou may use TypeToken to load the json string into a custom object. logs = gson.fromJson(br, new TypeToken
Documentation:
Kotlin: If you need to do it in Kotlin you can do it like this: val myType = object : TypeToken
Or you can see this answer for various alternatives. Your JSON sample is: { "status": "ok", "comment": "", "result": { "id": 276, "firstName": "mohamed", "lastName": "hussien", "players": [ "player 1", "player 2", "player 3", "player 4", "player 5" ] }so if you want to save arraylist of modules in your SharedPrefrences so : 1- will convert your returned arraylist for json format using this method public static String toJson(Object jsonObject) { return new Gson().toJson(jsonObject); }2- Save it in shared prefreneces PreferencesUtils.getInstance(context).setString("players", toJson((.....ArrayList you want to convert.....)));3- to retrieve it at any time get JsonString from Shared preferences like that 4- convert it again to array list public static Object fromJson(String jsonString, Type type) { return new Gson().fromJson(jsonString, type); } ArrayListthis solution also doable if you want to parse ArrayList of Objects Hope it's help you by using Gson Library . If you want to use Arrays, it's pretty simple. logs = gson.fromJson(br, JsonLog[].class); // line 6Provide the JsonLog as an array JsonLog[].class |