Jackson

ObjectMapper 通过 writeValue 系列方法 将 java 对 象序列化 为 json
ObjectMapper 通过 readValue 系列方法 将 json 反序列化为 java 对象。

json 转为 map

ObjectMapper objectMapper = new ObjectMapper();
String jsonObject ={\”brand \“:\”ford \“,\”doors \“:5};
Map <String,Object> jsonMap = objectMapper.readValue(jsonObject,
    new TypeReference <Map <String,Object >>(){};

json 转为 对象

ObjectMapper objectMapper = new ObjectMapper();
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";
Car car = objectMapper.readValue(carJson, Car.class);
Map<String, String> body = Collections.emptyMap();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setConnection("Close");//设置为短连接
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity(body, httpHeaders);
ResponseEntity<String> response = HttpInvoker.getInstance().postForEntity(uri, httpEntity, String.class);
Map<String, Object> bodyMap = (new ObjectMapper()).readValue(response.getBody(), new TypeReference<Map <String,Object >>(){});

你可能感兴趣的:(json)