电商网站网址,个人网站如何被百度收录,网站设计数据库怎么做,源码编辑器网页版目录 1. 需求2. 测试3. 实现需求4. 相关操作1. 将JSONObject装入JSONArray2. JSONArray与String的相互转换3. 注意#xff1a;toString与JSONObject.toJSONString的区别 1. 需求
最近有个需求#xff1a; 要接收某个接口的 JSON 数据#xff0c;而这个JSON数据有可能是一个… 目录 1. 需求2. 测试3. 实现需求4. 相关操作1. 将JSONObject装入JSONArray2. JSONArray与String的相互转换3. 注意toString与JSONObject.toJSONString的区别 1. 需求
最近有个需求 要接收某个接口的 JSON 数据而这个JSON数据有可能是一个 JSON 对象也有可能是一个 JSON数组。
{name,王五,age:10}[{name:张三,age:12},{name:李四,age:11}]现在呢我需要根据传递过来的 JSON 数据进行判断如果是对象就调用 resolve1()如果是数组就调用 resolve2()。
依赖 本文采用 fastjson 来处理 JSON 数据 dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.83/version/dependency因为要处理 JSON 数组所以要使用 JSONArray.parseArray
2. 测试
首先呢要判断 JSON 数据是否是一个数组那么我首先想到的是我用 JSONArray.parseArray 会不会出现异常如果出现了异常那我 try-catch 一下不是很简单就能实现了吗。
测试代码
/*** author chenjy* description:* date 2023/1/11*/
public class JSONTest {public static void main(String[] args) {String jsonArr [{name:张三,age:12},{name:李四,age:11}];String jsonObj {name:王五,age:10};JSONArray jsonArray JSONArray.parseArray(jsonArr);System.out.println(jsonArray);JSONArray jsonObject JSONArray.parseArray(jsonObj);System.out.println(jsonObject);}
}控制台输出
很显然JSONArray.parseArray 转换 JSON 对象 的时候会抛出异常 com.alibaba.fastjson.JSONException那么我们实现需求的思路就变得简单起来了。 兴趣使然我再来看一下 JSONObject.parseObject 能不能转换 JSON 数组。
测试代码
/*** author chenjy* description:* date 2023/1/11*/
public class JSONTest {public static void main(String[] args) {String jsonArr [{name:张三,age:12},{name:李四,age:11}];String jsonObj {name:王五,age:10};JSONObject jsonObject JSONObject.parseObject(jsonObj);System.out.println(jsonObject);JSONObject jsonArray JSONObject.parseObject(jsonArr);System.out.println(jsonArray);}
}控制台输出
果然也会抛出异常 com.alibaba.fastjson.JSONException。
3. 实现需求
好的经过上面的测试我们的需求实现思路在 try 中调用 resolve2在 catch 中调用 resolve1 。
/*** author chenjy* description:* date 2023/1/11*/
public class JSONTest {public static void main(String[] args) {String jsonArr [{name:张三,age:12},{name:李四,age:11}];String jsonObj {name:王五,age:10};System.out.println(测试 JSON 数组);getParam(jsonArr);System.out.println(测试 JSON 对象);getParam(jsonObj);}public static void getParam(String str) {try {JSONArray jsonArray JSONArray.parseArray(str);resolve2(jsonArray);} catch (JSONException e) {JSONObject jsonObject JSONObject.parseObject(str);resolve1(jsonObject);}}/*处理对象*/public static void resolve1(JSONObject obj) {System.out.println(姓名 obj.getString(name) 年龄 obj.get(age));}/*处理数组*/public static void resolve2(JSONArray array) {for (Object obj : array) {JSONObject jObj (JSONObject) JSON.toJSON(obj);System.out.println(姓名 jObj.getString(name) 年龄 jObj.get(age));}}
}4. 相关操作
1. 将JSONObject装入JSONArray
我来形象地说明一下两者的关系
JSONObject 就相当于一个 Map往 JSONObject 中新增键值对的方法 put(key, value)删除键值对的方法remove(key)。JSONArray 相当于一个 ListMap往 JSONObject 中新增元素的方法 add(JSONObject)删除键值对的方法remove(object)。
2. JSONArray与String的相互转换
String 转 JSONArray → JSONArray.parseArray(String str)JSONArray 转 String → jsonArray.toString()、jsonArray.toJSONString()、String.valueOf(jsonArray)
顺便说一下 JSONObject 和 String 之间的相互转换
String 转 JSONObject → JSONObject.parseObject(String str)JSONObject 转 String → jsonObject.toJSONString()、jsonObject.toString()、String.valueOf(jsonObject) String jsonArr [{name:张三,age:12},{name:李四,age:11}];String jsonObj {name:王五,age:10};JSONArray jsonArray JSONArray.parseArray(jsonArr);System.out.println(jsonArray.toJSONString() instanceof String jsonArray.toString() instanceof String String.valueOf(jsonArray) instanceof String);JSONObject jsonObject JSONObject.parseObject(jsonObj);System.out.println(jsonObject.toJSONString() instanceof String jsonObject.toString() instanceof String String.valueOf(jsonObject) instanceof String);3. 注意toString与JSONObject.toJSONString的区别
public class JSONTest {public static void main(String[] args) {Tom tom new Tom(张三, 18);System.out.println(tom.toString());JSONObject jsonObject JSONObject.parseObject(JSONObject.toJSONString(tom));System.out.println(jsonObject);jsonObject.put(sex, 1);System.out.println(jsonObject.toJSONString());}
}Data
AllArgsConstructor
class Tom {String name;Integer age;
}