java将对象以JOSN打印出来 writeValueAsString

封装成工具类

 

 
  1. public static String toJsonByObject(Object obj){

  2. String jsonStr = null;

  3. try {

  4. jsonStr = mapper.writeValueAsString(obj);

  5. } catch (IOException e) {

  6. // TODO Auto-generated catch block

  7. e.printStackTrace();

  8. }

  9. return jsonStr;

  10. }

  11.  

 

 

================================================

如何把Java对象转化成json字符串并打印出来呢?

这里就用到了jackon的jar包,使用writeValuesAsString的方法就可以把对角转化成json字符串。

下面通过一个demo,来仔细体会下如何去转化!

1:先建一个javaBean。

 

[java] view plain copy

  1. package com.sino.objectmapperdemo;  
  2.   
  3. public class BaseObject {  
  4.     private String userName;  
  5.     private String userCode;  
  6.     private double weight;  
  7.     private int height;  
  8.     private boolean sex;  
  9.     private String[] array;  
  10.     private BaseObject innerBaseObject;  
  11.       
  12.     public String getUserName() {  
  13.         return userName;  
  14.     }  
  15.     public void setUserName(String userName) {  
  16.         this.userName = userName;  
  17.     }  
  18.     public String getUserCode() {  
  19.         return userCode;  
  20.     }  
  21.     public void setUserCode(String userCode) {  
  22.         this.userCode = userCode;  
  23.     }  
  24.     public double getWeight() {  
  25.         return weight;  
  26.     }  
  27.     public void setWeight(double weight) {  
  28.         this.weight = weight;  
  29.     }  
  30.     public int getHeight() {  
  31.         return height;  
  32.     }  
  33.     public void setHeight(int height) {  
  34.         this.height = height;  
  35.     }  
  36.     public boolean isSex() {  
  37.         return sex;  
  38.     }  
  39.     public void setSex(boolean sex) {  
  40.         this.sex = sex;  
  41.     }  
  42.     public String[] getArray() {  
  43.         return array;  
  44.     }  
  45.     public void setArray(String[] array) {  
  46.         this.array = array;  
  47.     }  
  48.     public BaseObject getInnerBaseObject(){  
  49.         return innerBaseObject;  
  50.     }  
  51.     public void setInnerBaseObject(BaseObject innerBaseObject){  
  52.         this.innerBaseObject = innerBaseObject;  
  53.     }  
  54.       
  55.       
  56. }  

 

 

2:在MainActivity中,对其进行赋值和转化操作,并打印出来。

 

[java] view plain copy

  1. package com.sino.objectmapperdemo;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import org.codehaus.jackson.JsonGenerationException;  
  9. import org.codehaus.jackson.map.JsonMappingException;  
  10. import org.codehaus.jackson.map.ObjectMapper;  
  11.   
  12. import android.os.Bundle;  
  13. import android.app.Activity;  
  14. import android.view.Menu;  
  15. /** 
  16.  * 使用jackon的ObjectMapper的writeValueAsString方法可以把java对象转化成json字符串 
  17.  * 输出的结果是:{"array":["80","90","95"],"height":170,"innerBaseObject":{"array":["65","68","75"], 
  18.  * "height":171,"innerBaseObject":null,"userCode":"2号","userName":"李四","weight":75.5,"sex":true}, 
  19.  * "userCode":"1号","userName":"张三","weight":65.5,"sex":true} 
  20.  *  
  21.  */  
  22. public class MainActivity extends Activity {  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_main);  
  28.     try {  
  29.         ObjectMapper objectMapper = new ObjectMapper();  
  30. //      ArrayList list = new ArrayList();  
  31. //      Map map = new HashMap();  
  32.         BaseObject object1 = new BaseObject();  
  33.         object1.setUserName("张三");  
  34.         object1.setUserCode("1号");  
  35.         object1.setWeight(65.5);  
  36.         object1.setHeight(170);  
  37.         object1.setSex(true);  
  38.         String[] score={"80","90","95"};  
  39.         object1.setArray(score);  
  40.           
  41.         BaseObject object2=new BaseObject();    
  42.         object2.setUserName("李四");  
  43.         object2.setUserCode("2号");  
  44.         object2.setWeight(75.5);  
  45.         object2.setHeight(171);  
  46.         object2.setSex(true);   
  47.         score=new String[3];  
  48.         score[0]="65";  
  49.         score[1]="68";  
  50.         score[2]="75";  
  51.         object2.setArray(score);  
  52. //        object2成为object1的内部类  
  53.         object1.setInnerBaseObject(object2);  
  54. //        list.add(object1);   
  55. //        map.put("baseObject", list);    
  56. //        把对象转化成json字符串  
  57.         String json = objectMapper.writeValueAsString(object1);  
  58.         System.out.println(json);  
  59.         } catch (JsonGenerationException e) {  
  60.             e.printStackTrace();  
  61.         } catch (JsonMappingException e) {  
  62.             e.printStackTrace();  
  63.         } catch (IOException e) {  
  64.             e.printStackTrace();  
  65.         }  
  66.          
  67.           
  68.     }  
  69.   
  70.   
  71. }  

每天努力一点,总会有些收获。

你可能感兴趣的:(java将对象以JOSN打印出来 writeValueAsString)