欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java转JSON串的几种方式

程序员文章站 2023-12-17 18:13:58
昨天在与对端系统调接口的时候,对端系统对我们传过去的json串老是处理不了,后来查原因是应为我们传过去的json串里有json对象数组,因为我们的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":""} 
{"cardlist":[{"cardcode":"888888888","cardname":"bankcard1","cardvalue":99999999},{"cardcode":"999999999","cardname":"bankcard1","cardvalue":222222222}],"sex":"man","age":38,"email":""} 
{"cardlist":[{"cardname":"bankcard1","cardcode":"888888888","cardvalue":99999999},{"cardname":"bankcard2","cardcode":"999999999","cardvalue":222222222}],"sex":"man","age":38,"email":""}

上一篇:

下一篇: