java--09--대상과 JSON과 맵 사이의 전환
/** * JSON * @throws IOException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */
public static void beanCovertJSON() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
User user = new User();
user.setId(1);
user.setName("dema");
user.setPassword("123");
ObjectMapper objectMapper =new ObjectMapper();
// JSON
String JSONUser=objectMapper.writeValueAsString(user);
System.out.println(JSONUser);
}
@SuppressWarnings("deprecation")
public static void beanCovertJSON_02() throws IOException{
JsonGenerator jsonGenerator = null;
ObjectMapper objectMapper = null;
User user = new User();
objectMapper = new ObjectMapper();
//
jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
jsonGenerator.writeObject(user);
}
/** * * @throws JsonProcessingException */
public static void beanConvertByte() throws JsonProcessingException{
User user = new User();
user.setId(1);
user.setName("dema");
user.setPassword("123");
ObjectMapper objectMapper =new ObjectMapper();
byte[] b=objectMapper.writeValueAsBytes(user);
}
/** * JSON Map * @throws IOException * @throws JsonMappingException * @throws JsonParseException */
public static void JSONConvertMap() throws JsonParseException, JsonMappingException, IOException{
ObjectMapper objectMapper =new ObjectMapper();
String JSONUser="{\"id\":1,\"name\":\"dema\",\"password\":\"123\"}";
Map map=objectMapper.readValue(JSONUser, Map.class);
System.out.println(map.get("id"));
}
/** * Map JSON * @throws JsonProcessingException */
public static void MapConvertJSON() throws JsonProcessingException{
String str=null;
Map map=new HashMap();
map.put("1","a");
map.put("2", "b");
map.put("3", "c");
ObjectMapper objectMapper =new ObjectMapper();
//
str=objectMapper.writeValueAsString(map);
System.out.println(str);
}
참조 블로그:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html http://rsy.iteye.com/blog/2303323미완성 미속...
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콘텐츠 SaaS | JSON 스키마 양식 빌더Bloomreach Content를 위한 JSON Form Builder 맞춤형 통합을 개발합니다. 최근 Bloomreach Content SaaS는 내장 앱 프레임워크를 사용하여 혁신적인 콘텐츠 유형 필드를 구축할...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.