writeValueAsString

封装成工具类

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。

[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<String,Object> map = new HashMap<String,Object>();  
  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. }  
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值