封装成工具类
public static String toJsonByObject(Object obj){
String jsonStr = null;
try {
jsonStr = mapper.writeValueAsString(obj);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
================================================
如何把Java对象转化成json字符串并打印出来呢?
这里就用到了jackon的jar包,使用writeValuesAsString的方法就可以把对角转化成json字符串。
下面通过一个demo,来仔细体会下如何去转化!
1:先建一个javaBean。
- package com.sino.objectmapperdemo;
- public class BaseObject {
- private String userName;
- private String userCode;
- private double weight;
- private int height;
- private boolean sex;
- private String[] array;
- private BaseObject innerBaseObject;
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserCode() {
- return userCode;
- }
- public void setUserCode(String userCode) {
- this.userCode = userCode;
- }
- public double getWeight() {
- return weight;
- }
- public void setWeight(double weight) {
- this.weight = weight;
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- public boolean isSex() {
- return sex;
- }
- public void setSex(boolean sex) {
- this.sex = sex;
- }
- public String[] getArray() {
- return array;
- }
- public void setArray(String[] array) {
- this.array = array;
- }
- public BaseObject getInnerBaseObject(){
- return innerBaseObject;
- }
- public void setInnerBaseObject(BaseObject innerBaseObject){
- this.innerBaseObject = innerBaseObject;
- }
- }
- package com.sino.objectmapperdemo;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import org.codehaus.jackson.JsonGenerationException;
- import org.codehaus.jackson.map.JsonMappingException;
- import org.codehaus.jackson.map.ObjectMapper;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- /**
- * 使用jackon的ObjectMapper的writeValueAsString方法可以把java对象转化成json字符串
- * 输出的结果是:{"array":["80","90","95"],"height":170,"innerBaseObject":{"array":["65","68","75"],
- * "height":171,"innerBaseObject":null,"userCode":"2号","userName":"李四","weight":75.5,"sex":true},
- * "userCode":"1号","userName":"张三","weight":65.5,"sex":true}
- *
- */
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- try {
- ObjectMapper objectMapper = new ObjectMapper();
- // ArrayList list = new ArrayList();
- // Map<String,Object> map = new HashMap<String,Object>();
- BaseObject object1 = new BaseObject();
- object1.setUserName("张三");
- object1.setUserCode("1号");
- object1.setWeight(65.5);
- object1.setHeight(170);
- object1.setSex(true);
- String[] score={"80","90","95"};
- object1.setArray(score);
- BaseObject object2=new BaseObject();
- object2.setUserName("李四");
- object2.setUserCode("2号");
- object2.setWeight(75.5);
- object2.setHeight(171);
- object2.setSex(true);
- score=new String[3];
- score[0]="65";
- score[1]="68";
- score[2]="75";
- object2.setArray(score);
- // object2成为object1的内部类
- object1.setInnerBaseObject(object2);
- // list.add(object1);
- // map.put("baseObject", list);
- // 把对象转化成json字符串
- String json = objectMapper.writeValueAsString(object1);
- System.out.println(json);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }