java ObjectMapper json 与对象的相互转换

时间:2021-08-07 05:45:44

1 json string 转 JSONObject

 

try {
     JSONObject jsonObject = new JSONObject("{"phonetype":"N95","cat":"WP"}");
}catch (JSONException err){
     Log.d("Error", err.toString());
}

 

2 json string 转 JsonNode

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);

3 json string 装map

Map<String, String> map = mapper.readValue(jsonstring, Map.class);