string转jsonarray有双引号(Java把string转json格式的办法)
程序员文章站
2023-11-17 13:50:40
java对象与json字符串互相转换1.准备工作在与前端页面交互时,经常需要传递json字符串数据,我们可以使用jsonobject来处理json字符串。需要导入2个jar包:fastjson-1.2...
java对象与json字符串互相转换
1.准备工作
在与前端页面交互时,经常需要传递json字符串数据,我们可以使用jsonobject来处理json字符串。
需要导入2个jar包:
fastjson-1.2.2.jar,fastjson-1.2.2-sources.jar
import com.alibaba.fastjson.jsonobject;
import org.junit.test;
import java.util.*;
作为测试,先准备几个字符串数据:
private string s1 = " {\"areaname\": \"北京\",\n" +
" \"cityid\": 110100,\n" +
" \"nomarket\": false,\n" +
" \"oldcityid\": 646,\n" +
" \"pinyin\": \"beijing\",\n" +
" \"provinceid\": 110000}";
private string s2 = "[\n"+
" {\"name\":\"tom\",\"age\":18,\"gender\":\"男\"},\n"+
" {\"name\":\"lily\",\"age\":19,\"gender\":\"女\"},\n"+
" {\"name\":\"hanmeimei\",\"age\":17,\"gender\":\"女\"}]";
private string s3 = "{\"id\":1, \"from\":\"xiaoxin\", \n" +
" \"result\":[\n" +
" {\"age\":18,\"gender\":\"男\",\"name\":\"tom\"},\n" +
" {\"age\":19,\"gender\":\"女\",\"name\":\"lily\"},\n" +
" {\"age\":17,\"gender\":\"女\",\"name\":\"hanmeimei\"}]}";
private string s4 = "{\"age\":18,\"gender\":\"男\",\"name\":\"tom\"}";
private string s5 ="[1,2,3,4,5]";
2.字符串转对象
我们需要使用jsonobject.parse(),jsonobject.parsearray(),jsonobject.parseobject()三个方法来转换json数据,jsonobject.parse()获得后的数据需要强转为jsonobject才能进行下一步操作。
json字符串转为jsonobject对象后,操作数据与map类似,使用get()方法来获取数据,也可以使用getinteger(),getstring()…等方法来获取数据。
若要转换为java对象,则需要创建一个有相应字段的类,下例中为user类
@test
public void test1(){
//json字符串转json对象,与map类似
//只写parse,需要强转,写parseobject,则直接得到jsonobject
jsonobject jsonobject = (jsonobject) jsonobject.parse(s1);
system.out.println(jsonobject);//{"areaname":"北京","cityid":110100,"nomarket":false,"oldcityid":646,"pinyin":"beijing","provinceid":110000}
//从json对象中获取值
string areaname = jsonobject.getstring("areaname");
system.out.println("----------------");
system.out.println(areaname);
system.out.println(jsonobject.getinteger("oldcityid"));
system.out.println(jsonobject.get("cityid"));
system.out.println("----------------");
//遍历jsonobject
jsonobject.foreach((key,value)-> system.out.println(key+"->"+value));
//将字符串转换为java对象
user user = json.parseobject(s4, user.class);
system.out.println(user);
}
user类:
public class user {
private string name;
private int age;
private string gender;
@override
public string tostring() {
return "user{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public string getgender() {
return gender;
}
public void setgender(string gender) {
this.gender = gender;
}
public user() {
}
public user(string name, int age, string gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
转换json数组,需要使用 jsonobject.parsearray()方法,转换为jsonarray类,之后的操作方法与操作arraylist类似。
@test
public void test2(){
//json对象数组字符串转json对象数组,相当于arraylist
jsonarray jsons2 = jsonobject.parsearray(s2);
system.out.println(jsons2);//[{"age":18,"gender":"男","name":"tom"},{"age":19,"gender":"女","name":"lily"},{"age":17,"gender":"女","name":"hanmeimei"}]
//获取索引为1的值,需要强转为jsonobject
jsonobject o1 = (jsonobject)jsons2.get(0);
system.out.println(o1);
//遍历
// jsons2.foreach(item-> system.out.println(item));
jsons2.foreach(system.out::println);
//转化为java arraylist
list<user> users = json.parsearray(s2, user.class);
system.out.println(users);
}
转化复杂的json数据,需要一步一步转换,获取相应的数据可以使用getjsonarray(),getjsonobject()方法
@test
public void test3(){
//复杂数据转json对象,与上面的一样
jsonobject objects3 = jsonobject.parseobject(s3);
//拿到result jsonarray
jsonarray result = objects3.getjsonarray("result");
//遍历result
result.foreach(system.out::println);
//获取指定索引的值
jsonobject o = result.getjsonobject(1);
system.out.println(o.getstring("name")); //lily
//将json数组转换为list
list<integer> numarr = json.parsearray(s5, integer.class);
system.out.println(numarr);
//将result转换为list对象
list<user> users = json.parsearray(objects3.getjsonarray("result").tojsonstring(), user.class);
system.out.println(users);//[user{name='tom', age=18, gender='男'}, user{name='lily', age=19, gender='女'}, user{name='hanmeimei', age=17, gender='女'}]
}
3.对象转字符串
先准备一个map和一个list
private hashmap<string, string> map1 = new hashmap<string, string>();
private list<string> list1 = new arraylist<>();
private list<user> list2 = new arraylist<>();
使用jsonobject.tojsonstring()方法来将对象转换为json字符串
@test
public void test1(){
map1.put("apple","新鲜的苹果"); //向集合中添加对象
map1.put("computer","配置优良的计算机");
map1.put("book","堆积成山的图书");
system.out.println(map1);
//map对象转json字符串
string s = jsonobject.tojsonstring(map1);
system.out.println(s);
//java对象转json字符串
string s1 = jsonobject.tojsonstring(new user("tom", 18, "男"));
system.out.println(s1);
}
转换数组也是一样:
@test
public void test2(){
collections.addall(list1,"tom","18","男");
system.out.println(list1);
//arraylist转json字符串
string s = jsonobject.tojsonstring(list1);
system.out.println(s);
//java对象的arraylist转json字符串
collections.addall(list2,new user("tom",18,"男"),new user("lily",16,"女"));
system.out.println(jsonobject.tojsonstring(list2));
}