Java中json使用与问题汇总
hoje 人气:6一、JSON 解析类库
FastJson: 阿里巴巴开发的 JSON 库,性能十分优秀。
在maven项目的pom文件中以下依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> <https://img.qb5200.com/download-x/dependency>
二、编码与解码
1、编码
import com.alibaba.fastjson.JSONObject; import java.util.Arrays; import java.util.List; public class JsonTest1 { public static void main(String[] args) { JSONObject object = new JSONObject(); object.put("String","String"); object.put("Integer",1); object.put("boolean",true); List<Integer> list = Arrays.asList(1,2,3,4,5); object.put("list",list); object.put(null,null); System.out.println(object); } }
结果
{"Integer":1,"boolean":true,"String":"String","list":[1,2,3,4,5]}
2、解码
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.lang.annotation.ElementType; import java.util.Arrays; import java.util.List; public class JsonTest2 { public static void main(String[] args) { JSONObject object = JSONObject.parseObject("{\"boolean\":true,\"string\":\"string\",\"list\":[1,2,3],\"int\":2,\"test\":\"\"}"); //boolean Boolean bl = object.getBooleanValue("boolean"); System.out.println(bl); //String String string = object.getString("String"); System.out.println(string); //list List lts = JSON.parseArray(object.getJSONArray("list").toJSONString(), Integer.class); System.out.println(lts); for (Object i : lts) { System.out.println(i); } //Integer Integer integer = object.getIntValue("2"); System.out.println(integer); //没有这个key String aa = object.getString("AA"); System.out.println(aa); String bb = (String) object.get("aa"); System.out.println(bb); //对应的key没有value String dd = object.getString("test"); System.out.println(dd); String cc = object.get("test").toString(); System.out.println(cc); } }
结果
true null [1, 2, 3] 1 2 3 0 null null
最后2个的值为 " "
三、Json对象和字符串互相转换
https://www.runoob.com/w3cnote/java-json-instro.html
四、关于JSON.toJSONString()的问题
原文:https://blog.csdn.net/weixin_43228497/articlehttps://img.qb5200.com/download-x/details/87975659
4.1,第一种情况:
Activity activity=new Activity();
String str= JSON.toJSONString(activity);
此时,str是{}
第二种情况:
list list=new ArrayList();
String str= JSON.toJSONString(list);
此时,str是[]
第三种情况:
String str= JSON.toJSONString(null);
此时,str是null
4.2,怎么避免当list里面什么都没有的时候, JSON.toJSONString()之后是[]?
if(CollectionUtils.isEmpty(activityTypeDTOS)) {//加这个判断就可以了
五、JSON.toJSON().toString()与JSON.toJSONString()结果相同
import com.alibaba.fastjson.JSON; import com..socialsecurity.domain.model.ABA1Entity; import java.util.ArrayList; public class JsonTest { public static void main(String[] args) { ABA1Entity aba1Entity1 = new ABA1Entity(); aba1Entity1.setAac002("2"); ABA1Entity aba1Entity2 = new ABA1Entity(); aba1Entity2.setAac002("3"); List<ABA1Entity> aba1EntityList = new ArrayList<>(); aba1EntityList.add(aba1Entity1); aba1EntityList.add(aba1Entity2); String json = JSON.toJSON(aba1EntityList).toString(); String json1 =JSON.toJSONString(aba1EntityList); System.out.println(json); System.out.println(json1); } }
结果
[{"aac002":"2"},{"aac002":"3"}] [{"aac002":"2"},{"aac002":"3"}]
六、StringEscapeUtils.unescapeJava(jsonStr)去掉转义符
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.socialsecurity.domain.model.Person2; import groovy.json.StringEscapeUtils; import java.util.HashMap; import java.util.List; import java.util.Map; public class Test { public static void main(String[] args) { List data = null; Map<String, String> resultMap = new HashMap<>(); JSONArray objArray = new JSONArray(); resultMap.put("age", "aa"); resultMap.put("name", "bb"); String jsonObject = JSON.toJSONString(resultMap); objArray.add(jsonObject); System.out.println("objArray:"+objArray); data = objArray; String json = JSON.toJSON(data).toString(); System.out.println(json); if (data.size() == 1) { JSONObject json3 = JSONObject.parseObject((String) data.get(0)); String jsonStr = JSON.toJSON(json3).toString(); String json4 = StringEscapeUtils.unescapeJava(jsonStr); System.out.println("data.get(0):"+jsonStr); System.out.println("StringEscapeUtils.unescapeJava(jsonStr):"+json4); } Person2 person = new Person2(); person.setName("bb"); person.setAge("aa"); String json1 = JSON.toJSON(person).toString(); System.out.println("person:"+json1); } }
加载全部内容