[프로젝트 실전] 잭 슨 프레임 워 크 의 자바 대상 과 제 이 슨 데이터 변환 실현

17596 단어 json
  :
Jackson      Java     json   xmljsonxml   Java

공식 위 키: http://wiki.fasterxml.com/JacksonInFiveMinutes
공식 문서: http://wiki.fasterxml.com/JacksonDocumentation
github:https://github.com/FasterXML/jackson
 
 

jackson-databind http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/
jar  
 jackson-core   jackson-annotations
 github:
https://github.com/FasterXML/jackson-annotations

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/


1java   JSON 
/***
     * json clazz (          )
     */
    public static Object jsonToObject(String jsonString, Class clazz){
		ObjectMapper mapper = new ObjectMapper();
		try {
		    return mapper.readValue(jsonString, clazz);
		}catch (Exception e) {
		    // TODO Auto-generated catch block
		}
    }
 2json java  
 json[    ]Jackson    java   json      _ 1   
    /***
     * json clazz (          )
     */
    public static Object jsonToObject(String jsonString, Class clazz){
		ObjectMapper mapper = new ObjectMapper();
		try {
		    return mapper.readValue(jsonString, clazz);
		}catch (Exception e) {
		    // TODO Auto-generated catch block
		}
    }
 
3java   xml(   stax2-api.jar)
XmlMapper xml = new XmlMapper();
StringWriter sw = new StringWriter();
xml.writeValue(sw, bean);
 
4json     
    /**
     *                json            
     * @param data :json   
     * @param nodeName :        
     * @param cls:         
     * @return
     */
    public static Object parseJsonToObject(String data, String nodeName, Class<?> cls) {
		//       Json    
		Object object = null;
		ObjectMapper mapper = new ObjectMapper();
		try {
		    JsonNode rootNode = mapper.readTree(data); //   Json
		    // rootNode.path("xx")       JsonNode  ,   JsonNode     ,       
		    JsonNode strs = rootNode.path(nodeName);
		    // JsonNode strs1 = rootNode.get(nodeName);
		    if (!"".equals(strs)) {
		    	object = mapper.readValue(strs.toString(), cls);
		    }
		} catch (Exception e) {
		    
		}
		return object;
    }


    /**
     *     json          
     * @param data:json   
     * @param nodeName:        
     * @return
     */
    public static String parseJson(String data, String nodeName) {
		//       Json    
		StringBuffer buf = new StringBuffer();
		ObjectMapper mapper = new ObjectMapper();
		JsonNode rootNode;
		try {
		    rootNode = mapper.readTree(data);
		    //   Json
		    // rootNode.path("xx")       JsonNode  ,   JsonNode     ,       
		    String strs = "";
		    if(rootNode.path(nodeName).getClass()==ObjectNode.class){
			    strs = (rootNode.path(nodeName)==null ? "" :rootNode.path(nodeName)).toString();
		    }else{
		    	strs = rootNode.path(nodeName).asText();// JsonNode strs1 =rootNode.get(nodeName);
		    }
		    buf.append(strs);
		} catch (Exception e) {
		    e.printStackTrace();
		}
		return buf.toString();
    }
 
5json List
    /**
     * json List
     * @param data:json  
     * @param nodeName:        
     * @param clazz:         
     * @return
     */
    public static <T> List<T> parseJsonToList(String data, String nodeName, Class<?> clazz) {
		List<T> list = null;
		TypeFactory t = TypeFactory.defaultInstance();
		//          (   ArrayList clazz)
		ObjectMapper mapper = new ObjectMapper();
		JsonNode rootNode;
		try {
		    rootNode = mapper.readTree(data);//   Json
		    // rootNode.path("xx")       JsonNode  ,   JsonNode     ,       
		    if (null != rootNode.get(nodeName)) {
				JsonNode strs = rootNode.path(nodeName);
				// JsonNode strs1 = // rootNode.get(nodeName);
				list = mapper.readValue(strs.toString(),t.constructCollectionType(ArrayList.class, clazz));
		    }
		    //   T               :
		    // List<T> list = mapper.readValue(jsonVal, new TypeReference<List<T>>() {});
		} catch (IOException e) {
		}
		return list;
    }
 

 1.json  key    java    field
 2.Date  long

좋은 웹페이지 즐겨찾기