類似 Json.Net ,能夠直接將 json string 直接轉換成對應的model class

------------- MainActivity-------------

  1     private void initView()
  2     { 
  3         TextView textView  = (TextView)findViewById(R.id.textView); 
  4          
  5         ObjectMapper mapper = new ObjectMapper();
  6  
  7         try 
  8         { 
  9             String json = "{\"name\":\"Hello Jackson\"}"; 
 10             TestModel model = mapper.readValue(json, TestModel.class); 
 11  
 12             textView.setText(model.getName()); 
 13              
 14         } catch (JsonParseException e) 
 15         { 
 16             // TODO Auto-generated catch block 
 17             e.printStackTrace(); 
 18         } catch (JsonMappingException e) 
 19         { 
 20             // TODO Auto-generated catch block 
 21             e.printStackTrace(); 
 22         } catch (IOException e) 
 23         { 
 24             // TODO Auto-generated catch block 
 25             e.printStackTrace(); 
 26         } 
 27  
 28     }

------------- TestModel -------------

 

  1 public class TestModel
  2 { 
  3     private String name; 
  4  
  5     public String getName() 
  6     { 
  7         return name; 
  8     } 
  9      
 10     public void setName(String name)
 11     { 
 12         this.name = name; 
 13     } 
 14 }


發現一個很棒的網站,能夠將Code直接轉換成html

http://puzzleware.net/codehtmler/default.aspx

arrow
arrow
    全站熱搜

    小賢 發表在 痞客邦 留言(0) 人氣()