以下总结一下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 cardList=new ArrayList();
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 getCardList() {
return cardList;
}
public void setCardList(List 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 cards=new ArrayList();
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包
com.alibabagroupId>
fastjsonartifactId>
1.2.15version>
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教学资源。帮助大家在成为Java架构师的道路上披荆斩棘。需要资料的欢迎加入学习交流群:9285,05736
网友评论