Object转String,String转Map

实体里重写toString方法,符合json格式 

@Override
  public String toString() {
    return "{" +
        "id='" + id + '\'' +
        ", name='" + name + '\'' +
        ", pid='" + pid + '\'' +
        '}';
  }

测试类 

@Test
	public void testStringMap() {
		Menu menu = new Menu();
		menu.setId("1");
		menu.setName("2");
		menu.setPid("3");
		Map res = null;
		Gson gson = new Gson();
		res = gson.fromJson(menu.toString(), new TypeToken>() {
		}.getType());
		for (String key : res.keySet()) {
			System.out.println(res.get(key));
		}
	}

 maven

   
    
        com.google.code.gson
        gson
        2.2.4
    

输出:

1
2
3

 

你可能感兴趣的:(JAVAUtils)