Code example for Gson

Methods: toJson

0
	public void testMap2String() { 
		HashMap<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("name", "name1");
		map.put("age", 1);
		// System.out.println(map); 
		Gson gson = new Gson();
		String str = gson.toJson(map);
		// System.out.println(str); 
		Assert.assertEquals("{\"name\":\"name1\",\"age\":1}", str);
	} 
 
	@Test 
	public void testNestedMap2String() { 
		HashMap<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("name", "name1");
		map.put("age", 1);
		HashMap<String, Object> nestedMap = new LinkedHashMap<String, Object>();
		nestedMap.put("name", "name1");
		nestedMap.put("age", 1);
		map.put("map", nestedMap);