Created
May 15, 2017 18:25
-
-
Save rmondragon/44ad90279f5290c25332cf42fba1b49f to your computer and use it in GitHub Desktop.
JSON to List or Map using GSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Map; | |
import com.google.gson.Gson; | |
import com.google.gson.reflect.TypeToken; | |
import javax.ws.rs.core.GenericType; | |
public class JsonGsonToListOrMap { | |
public static void main(String[] args) { | |
String s; | |
List<Object> list; | |
Gson gson = new Gson(); | |
// works | |
s = "{\"url\":{\"cpm\":[{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"},{\"image\":\"https://i.rmbl.ws/s8/6/g/t/n/t/gtnta.4Wpjb.2.jpg\",\"score\":700000000000,\"boosted\":false,\"headline\":\"Fat cat fails to rescue feline friend\",\"url\":\"http://rumble.com/v2zx6u-fat-cat-fails-to-rescue-feline-friend.html\"}]}}"; | |
Map<String, Map<String, List<Object>>> mapD= gson.fromJson(s, new GenericType<Map<String, Map<String, List<Object>>>>(){}.getType()); | |
mapD.forEach((x,y) -> System.out.println("key: " + x +", value: "+ y)); | |
// works | |
s = "{\"url\":{\"cpm\":[{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"},{\"image\":\"https://i.rmbl.ws/s8/6/g/t/n/t/gtnta.4Wpjb.2.jpg\",\"score\":700000000000,\"boosted\":false,\"headline\":\"Fat cat fails to rescue feline friend\",\"url\":\"http://rumble.com/v2zx6u-fat-cat-fails-to-rescue-feline-friend.html\"}]}}"; | |
Map<String, Map<String, List<Object>>> mapC= gson.fromJson(s, new TypeToken<Map<String, Map<String, List<Object>>>>(){}.getType()); | |
mapC.forEach((x,y) -> System.out.println("key: " + x +", value: "+ y)); | |
// works | |
s = "{\"cpm\":[{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"},{\"image\":\"https://i.rmbl.ws/s8/6/g/t/n/t/gtnta.4Wpjb.2.jpg\",\"score\":700000000000,\"boosted\":false,\"headline\":\"Fat cat fails to rescue feline friend\",\"url\":\"http://rumble.com/v2zx6u-fat-cat-fails-to-rescue-feline-friend.html\"}]}"; | |
Map<String, List<Object>> mapB= gson.fromJson(s, new TypeToken<Map<String, List<Object>>>(){}.getType()); | |
mapB.forEach((x,y) -> System.out.println("key: " + x +", value: "+ y)); | |
// works | |
s = "[{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"}]"; | |
list= gson.fromJson(s, new TypeToken<List<Object>>(){}.getType()); | |
list.forEach(x -> System.out.println(x)); | |
// works | |
s = "[{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"}]"; | |
list= gson.fromJson(s, new TypeToken<List<Object>>(){}.getType()); | |
list.forEach(x -> System.out.println(x)); | |
// works | |
s = "{\"image\":\"https://i.rmbl.ws/s8/6/1/R/V/u/1RVua.qR4e.2.jpg\",\"score\":1250000000000,\"boosted\":false,\"headline\":\"Puppy vs. Monster - 38k51\",\"url\":\"http://rumble.com/v38k51-puppy-vs.-monster.html\"}"; | |
Map<String, Object> mapA= gson.fromJson(s, new TypeToken<Map<String, Object>>(){}.getType()); | |
mapA.forEach((x,y) -> System.out.println("key: " + x +", value: "+ y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment