java转json_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 st.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;
}
json转换对象public String getEmail() {
return email;
}
public void setEmail(String 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 st.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 st.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 st.jsontest.pojo.Card;
import st.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");
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(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");
/
/通过JSONArray包装对象数组
JSONArray jArray=new JSONArray();
jArray.addAll(cards);
jObject.put("cardList", jArray);
String JSONString();
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");
//构造JSON字符串
String cardjsonStr1="{\"cardName\":\"bankCard1\",\"cardCode\":\"888888888\",\"cardValue\":99999999}";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论