添加依赖
build.gradle
//Gson解析
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
通过Gson将Json字符串转对象
String prames = "{'name':'大哥','age':'30'}"
JsonObject jsonObject = new JsonParser().parse(prames).getAsJsonObject();
String name = jsonObject.get("name").getAsString();
String age = jsonObject.get("age").getAsString();
通过Gson将Json对象转字符串
Map<String,String> map = new HashMap<String, String>();
map.put("name","大哥");
map.put("age","122");
String jsonString = new Gson().toJson(map);
网友评论