if(scenicInfo.getKindsImage() != null){
List<String> imageUrl = JSONArray.parseArray(scenicInfo.getKindsImage(),String.class);
scenicInfo.setKindsImage(imageUrl.get(0));
}
(1)parseArray的作用
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.junit.Test;
import java.util.List;
public class JSONTest {
@Test
public void JSONTest() {
//从Redis中查出来的字符串
String str = "[{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
"pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
""pos_p_e_total_subQuantity":"xxx"}]";
//将字符串转换成list
List<Emp> parseArray = JSON.parseArray(str, Emp.class);
for (Emp emp : parseArray){
System.out.println(emp);
}
}
}
@Data
class Emp {
private String ct_pt;
private String data_time;
private String mp_id;
private String mp_name;
private String pos_p_e_total_e;
private String pos_p_e_total_s;
private String pos_p_e_total_sub;
private String pos_p_e_total_subQuantity;
}
输出结果:
(2)toJSONString的作用
package com.navict.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JSONTest {
@Test
public void JSONTest() {
List<Emp> list = new ArrayList<>();
Emp emp = new Emp();
emp.setCt_pt("1");
emp.setData_time(new Date());
emp.setMp_id("1");
emp.setMp_name("李四");
emp.setPos_p_e_total_e("1");
list.add(emp);
//将集合转换成字符串
String str = JSON.toJSONString(list);
System.out.println(str);
}
}
@Data
class Emp {
private String ct_pt;
private Date data_time;
private String mp_id;
private String mp_name;
private String pos_p_e_total_e;
}
运行结果
(3)ParseObject的作用
package com.navict.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JSONTest {
@Test
public void JSONTest() {
String str = "{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
"pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
""pos_p_e_total_subQuantity":"xxx"}";
//将字符串转换成对象
JSONObject jsonObject = JSONObject.parseObject(str);
String ct_pt = (String)jsonObject.get("ct_pt");
String mp_id = (String)jsonObject.get("ct_pt");
// Date data_time =(Date) jsonObject.get("data_time");
String mp_name = (String)jsonObject.get("mp_name");
String pos_p_e_total_e = (String)jsonObject.get("pos_p_e_total_e");
Emp testEmp = new Emp();
testEmp.setMp_id(mp_id);
testEmp.setPos_p_e_total_e(pos_p_e_total_e);
testEmp.setMp_name(mp_name);
//testEmp.setData_time(data_time);
testEmp.setCt_pt(ct_pt);
System.out.println(JSONObject.toJSON(testEmp));
System.out.println(jsonObject.get("ct_pt"));
}
}
@Data
class Emp {
private String ct_pt;
private Date data_time;
private String mp_id;
private String mp_name;
private String pos_p_e_total_e;
}
(4) com.alibaba.fast.JSONArray的作用
package com.navict.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JSONTest {
@Test
public void JSONTest() {
String str = "{"ct_pt":"xxx","data_time":"xxx","mp_id":" +
""xxx","mp_name":"xxx","pos_p_e_total_e":"xxx","" +
"pos_p_e_total_s":"xxx","pos_p_e_total_sub":"xxx"," +
""pos_p_e_total_subQuantity":"xxx"}";
//将字符串转换成对象
Emp emp = JSONArray.parseObject(str, Emp.class);
System.out.println(JSONObject.toJSONString(emp));
}
}
@Data
class Emp {
private String ct_pt;
private Date data_time;
private String mp_id;
private String mp_name;
private String pos_p_e_total_e;
}
运行结果:
————————————————
原文链接:https://blog.csdn.net/hukehukehukehuke/article/details/106198340