Java转JSON串的几种方式

昨天在与对端系统调接口的时候,对端系统对我们传过去的json串老是处理不了,后来查原因是应为我们传过去的json串里有json对象数组,因为我们的json串存在表里的,取出来是作为json字符串放到json数组里的,所以带了双引号,对端认为是字符串,不是json对象所以处理不了。[{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999},{"cardName":"bankCard2","cardCode":"999999999","cardValue":222222222}] 这种是正确的json对象数组,["{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999}","{"cardName":"bankCard2","cardCode":"999999999","cardValue":222222222}"],这种是json字符串数组。

以下总结一下java转JSON串的几种方式:

1、将java对象转成json串

2、通过JSONObject生成json串

3、通过json字符串生成json串

代码通过阿里的fastjson包实现。

代码如下:

1、Person类,包含Crad的List

package com.doit8.test.jsontest.pojo;  
 
import java.util.ArrayList;  
import java.util.List;  
 
public class Person {  
   private String username;  
   private String email;  
   private String sex;  
   private int age;  
   private List<Card> cardList=new ArrayList<Card>();  
     
   public String getUsername() {  
       return username;  
   }  
   public void setUsername(String username) {  
       this.username = username;  
   }  
   public String getEmail() {  
       return email;  
   }  
   public void setEmail(String email) {  
       this.email = email;  
   }  
   public String getSex() {  
       return sex;  
   }  
   public void setSex(String sex) {  
       this.sex = sex;  
   }  
   public int getAge() {  
       return age;  
   }  
   public void setAge(int age) {  
       this.age = age;  
   }  
   public List<Card> getCardList() {  
       return cardList;  
   }  
   public void setCardList(List<Card> cardList) {  
       this.cardList = cardList;  
   }  
 
}

2、Card类

package com.doit8.test.jsontest.pojo;  
 
public class Card {  
   private String cardName;  
   private String cardCode;  
   private Integer cardValue;  
   public String getCardName() {  
       return cardName;  
   }  
   public void setCardName(String cardName) {  
       this.cardName = cardName;  
   }  
   public String getCardCode() {  
       return cardCode;  
   }  
   public void setCardCode(String cardCode) {  
       this.cardCode = cardCode;  
   }  
   public Integer getCardValue() {  
       return cardValue;  
   }  
   public void setCardValue(Integer cardValue) {  
       this.cardValue = cardValue;  
   }  
}

3、JSON处理测试类

package com.doit8.test.jsontest;  
 
import java.util.ArrayList;  
import java.util.List;  
 
import com.alibaba.fastjson.JSON;  
import com.alibaba.fastjson.JSONArray;  
import com.alibaba.fastjson.JSONObject;  
import com.doit8.test.jsontest.pojo.Card;  
import com.doit8.test.jsontest.pojo.Person;  
 
/** 
* JSON转换 
* 
*/  
public class App   
{  
   public static void main( String[] args )  
   {  
       //1。通过对象生成JSON串,对象里包含对象数组转成JSON串。  
       Person person=new Person();  
       person.setUsername("xiejava");  
       person.setSex("man");  
       person.setAge(38);  
       person.setEmail("xiejava@ishareread.com");  
       Card card1=new Card();  
       card1.setCardName("bankCard1");  
       card1.setCardCode("888888888");  
       card1.setCardValue(99999999);  
       Card card2=new Card();  
       card2.setCardName("bankCard1");  
       card2.setCardCode("999999999");  
       card2.setCardValue(222222222);  
       //对象数组  
       List<Card> cards=new ArrayList<Card>();  
       cards.add(card1);  
       cards.add(card2);  
       person.setCardList(cards);  
       String json = JSON.toJSON(person).toString();  
       System.out.println(json);  
         
       //2.通过JSON对象生成JSON串  
       JSONObject jObject=new JSONObject();  
       jObject.put("username", "xiejava");  
       jObject.put("sex", "man");  
       jObject.put("age", 38);  
       jObject.put("email", "xiejava@ishareread.com");  
       //通过JSONArray包装对象数组  
       JSONArray jArray=new JSONArray();  
       jArray.addAll(cards);  
       jObject.put("cardList", jArray);  
       String json2=jObject.toJSONString();  
       System.out.println(json2);  
         
         
       //3.通过JSON对象生成JSON串  
       JSONObject jObject2=new JSONObject();  
       jObject2.put("username", "xiejava");  
       jObject2.put("sex", "man");  
       jObject2.put("age", 38);  
       jObject2.put("email", "xiejava@ishareread.com");  
       //构造JSON字符串  
       String cardjsonStr1="{\"cardName\":\"bankCard1\",\"cardCode\":\"888888888\",\"cardValue\":99999999}";  
       String cardjsonStr2="{\"cardName\":\"bankCard2\",\"cardCode\":\"999999999\",\"cardValue\":222222222}";  
       JSON.parseObject(cardjsonStr1);  
       JSONArray jArray2=new JSONArray();  
       //将JSON字符串转成JSON对象,加入到JSONArray,[注意一定要用JSON.parseObject()方法转换成JSON对象,否则还是字符串,转成JSON串的时候会带双引号。]  
       jArray2.add(JSON.parseObject(cardjsonStr1));  
       jArray2.add(JSON.parseObject(cardjsonStr2));  
       jObject2.put("cardList", jArray2);  
       String json3=jObject2.toJSONString();  
       System.out.println(json3);  
         
         
   }  
}

pom.xml中引入fastjson包

<dependency>  
   <groupId>com.alibaba</groupId>  
   <artifactId>fastjson</artifactId>  
   <version>1.2.15</version>  
</dependency>

运行结果

{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999},{"cardName":"bankCard1","cardCode":"999999999","cardValue":222222222}],"sex":"man","age":38,"email":"xiejava@ishareread.com","username":"xiejava"}  
{"cardList":[{"cardCode":"888888888","cardName":"bankCard1","cardValue":99999999},{"cardCode":"999999999","cardName":"bankCard1","cardValue":222222222}],"sex":"man","age":38,"email":"xiejava@ishareread.com","username":"xiejava"}  
{"cardList":[{"cardName":"bankCard1","cardCode":"888888888","cardValue":99999999},{"cardName":"bankCard2","cardCode":"999999999","cardValue":222222222}],"sex":"man","age":38,"email":"xiejava@ishareread.com","username":"xiejava"}

原文发布于微信公众号 - java工会(javagonghui)

原文发表时间:2018-05-27

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

java9系列(五)Stack-Walking API

java9新增这个类的目的是提供一个标准API用于访问当前线程栈,之前只有Throwable::getStackTrace、Thread::getStackTr...

411
来自专栏闵开慧

Failed to set permissions of path

12/09/04 16:46:34 INFO support.ClassPathXmlApplicationContext: Refreshing org...

2707
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1142
来自专栏Hadoop数据仓库

Oracle sqlldr 如何导入一个日期列

1. LOAD DATA INFILE * INTO TABLE test FIELDS TERMINATED BY X'9' TRAILING NULLCO...

1786
来自专栏跟着阿笨一起玩NET

c# 使用timer定时器操作,上次定时到了以后,下次还未执行完怎么处理

------解决方案-------------------------------------------------------- 开始的时候,禁用定时器,你...

2601
来自专栏前端儿

Web 前端颜色值--字体--使用,整理整理

颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义。对光源进行设置的最低值可以是 0(十六进制 00)。最高值是 2...

2202
来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

7415
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

5866
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1112
来自专栏余生开发

echarts太阳分布图-饼图来回穿梭

var dom = document.getElementById("container");

1152

扫码关注云+社区