paulwong

          JAVABEAN和JSON STRING互轉(zhuǎn)

          TestUser.java
          import javax.xml.bind.annotation.XmlRootElement;

          import org.codehaus.jackson.annotate.JsonProperty;

          @XmlRootElement
          public class TestUser {
              
              /*@XmlElement(name="username")*/
              @JsonProperty("username")
              private String userName;
              
              private String email;

              public String getEmail() {
                  return email;
              }

              public String getUserName() {
                  return userName;
              }

              public void setUserName(String userName) {
                  this.userName = userName;
              }

              public void setEmail(String email) {
                  this.email = email;
              }

              @Override
              public String toString() {
                  return "TestUser [userName=" + userName + ", email=" + email + "]";
              }
              
              

          }


          Test.java
          import java.io.IOException;

          import org.codehaus.jackson.JsonParseException;
          import org.codehaus.jackson.map.DeserializationConfig;
          import org.codehaus.jackson.map.JsonMappingException;
          import org.codehaus.jackson.map.ObjectMapper;
          import org.codehaus.jackson.type.TypeReference;
          import org.junit.Test;
          import org.slf4j.Logger;
          import org.slf4j.LoggerFactory;


          public class Test {
              
              private Logger logger = LoggerFactory.getLogger(Test.class);
              
              /*private String getBaseUri()
              {
                  return "
          http://192.168.1.223:8080/restfull-api";
              }
          */
              
              @Test
              public void testTest()
              {
                  ObjectMapper mapper = new ObjectMapper();
                  /*AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
                  // make deserializer use JAXB annotations (only)
                  mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
                  // make serializer use JAXB annotations (only)
                  mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
          */
                  mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                  
                  String userStr = "{\"username\":\"paul\",\"email\":\"paul@paul.com\"}";
                  try {
                      TypeReference<TestUser> temp = new TypeReference<TestUser>(){};
                      TestUser testUser = mapper.readValue(userStr, temp);
                      System.out.println(testUser.toString());
                      
                      String result = mapper.writeValueAsString(testUser);
                      System.out.println(result);
                      
                  } catch (JsonParseException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (JsonMappingException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }

          }









          posted on 2014-07-04 09:07 paulwong 閱讀(585) 評(píng)論(0)  編輯  收藏 所屬分類: JAX-RS


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 天柱县| 家居| 云安县| 凤冈县| 曲松县| 武汉市| 五家渠市| 铜陵市| 临朐县| 涪陵区| 平凉市| 台江县| 泽库县| 纳雍县| 马关县| 十堰市| 高密市| 盘山县| 和政县| 永胜县| 施秉县| 泗水县| 花莲市| 贵州省| 泗洪县| 蓬溪县| 新乐市| 扶沟县| 潼关县| 汉川市| 平利县| 托克托县| 会同县| 鲜城| 神池县| 淄博市| 临澧县| 辉县市| 祁门县| 江都市| 呼伦贝尔市|