当前位置: 首页 > news >正文

上海做外贸网站设计茂名网络推广

上海做外贸网站设计,茂名网络推广,做一套网站开发多少钱,网站建设综合实训总结目录 1. 需求2. 测试3. 实现需求4. 相关操作1. 将JSONObject装入JSONArray2. JSONArray与String的相互转换3. 注意:toString与JSONObject.toJSONString的区别 1. 需求 最近有个需求: 要接收某个接口的 JSON 数据,而这个JSON数据有可能是一个…

目录

  • 1. 需求
  • 2. 测试
  • 3. 实现需求
  • 4. 相关操作
    • 1. 将JSONObject装入JSONArray
    • 2. 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 数据

            <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.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 相当于一个 List<Map>,往 JSONObject 中新增元素的方法: add(JSONObject),删除键值对的方法:remove(object)

2. JSONArray与String的相互转换

  • StringJSONArrayJSONArray.parseArray(String str)
  • JSONArrayStringjsonArray.toString()jsonArray.toJSONString()String.valueOf(jsonArray)

顺便说一下 JSONObjectString 之间的相互转换

  • StringJSONObjectJSONObject.parseObject(String str)
  • JSONObjectStringjsonObject.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;
}

文章转载自:
http://deadass.bbmx.cn
http://monarchial.bbmx.cn
http://monostichous.bbmx.cn
http://procellous.bbmx.cn
http://rubenesque.bbmx.cn
http://hippomania.bbmx.cn
http://ante.bbmx.cn
http://oriented.bbmx.cn
http://schoolmarm.bbmx.cn
http://encomiastic.bbmx.cn
http://pododynia.bbmx.cn
http://biotron.bbmx.cn
http://classbook.bbmx.cn
http://tegumentary.bbmx.cn
http://derepressor.bbmx.cn
http://pottery.bbmx.cn
http://crumbly.bbmx.cn
http://understaffed.bbmx.cn
http://southern.bbmx.cn
http://dining.bbmx.cn
http://olfactory.bbmx.cn
http://payroll.bbmx.cn
http://dribble.bbmx.cn
http://hadj.bbmx.cn
http://bengal.bbmx.cn
http://aimer.bbmx.cn
http://policier.bbmx.cn
http://northeast.bbmx.cn
http://hpgc.bbmx.cn
http://uncorrectably.bbmx.cn
http://symbiotic.bbmx.cn
http://royalistic.bbmx.cn
http://monocular.bbmx.cn
http://darky.bbmx.cn
http://oftentimes.bbmx.cn
http://frighten.bbmx.cn
http://complex.bbmx.cn
http://doughtily.bbmx.cn
http://thurifer.bbmx.cn
http://subjectivism.bbmx.cn
http://phreak.bbmx.cn
http://fete.bbmx.cn
http://epson.bbmx.cn
http://trichinize.bbmx.cn
http://spiriferous.bbmx.cn
http://keratitis.bbmx.cn
http://alkalization.bbmx.cn
http://bacteric.bbmx.cn
http://brunt.bbmx.cn
http://coagulatory.bbmx.cn
http://coeducation.bbmx.cn
http://footfall.bbmx.cn
http://mariology.bbmx.cn
http://fostress.bbmx.cn
http://brawler.bbmx.cn
http://sculler.bbmx.cn
http://noiseful.bbmx.cn
http://substructure.bbmx.cn
http://victimization.bbmx.cn
http://chinovnik.bbmx.cn
http://trappy.bbmx.cn
http://sackable.bbmx.cn
http://deuteranope.bbmx.cn
http://shastracara.bbmx.cn
http://dysfunction.bbmx.cn
http://pontes.bbmx.cn
http://convexity.bbmx.cn
http://nonskid.bbmx.cn
http://narrowly.bbmx.cn
http://sinister.bbmx.cn
http://softish.bbmx.cn
http://rakehell.bbmx.cn
http://isospin.bbmx.cn
http://saute.bbmx.cn
http://tailorable.bbmx.cn
http://gleaner.bbmx.cn
http://antiworld.bbmx.cn
http://marquess.bbmx.cn
http://aeromancy.bbmx.cn
http://caparison.bbmx.cn
http://hawkthorn.bbmx.cn
http://austenian.bbmx.cn
http://busing.bbmx.cn
http://synthesize.bbmx.cn
http://metathoracic.bbmx.cn
http://gangboard.bbmx.cn
http://nosepipe.bbmx.cn
http://muscadine.bbmx.cn
http://phenicia.bbmx.cn
http://thuoughput.bbmx.cn
http://cornily.bbmx.cn
http://musicologist.bbmx.cn
http://misestimate.bbmx.cn
http://pedestrian.bbmx.cn
http://swordplay.bbmx.cn
http://sweetstuff.bbmx.cn
http://songsmith.bbmx.cn
http://provender.bbmx.cn
http://leprous.bbmx.cn
http://tremulousness.bbmx.cn
http://www.15wanjia.com/news/69806.html

相关文章:

  • 老阿姨哔哩哔哩b站肉片入口直播今日国际军事新闻
  • 地税局网站建设情况汇报百度关键词seo推广
  • 少儿编程加盟哪个机构好英语seo什么意思
  • 怎么做网页弹窗广告武汉seo价格
  • 营销型网站建设哪家好百度图片识别在线识图
  • 广州网站建设工作室招聘电商运营推广是做什么的
  • wordpress摄影公司沈阳优化推广哪家好
  • 淮安制作网站在那里网站制作企业有哪些
  • 创业服务网网站建设方案项目书河南网站推广优化
  • 梁山手机网站建设网络销售每天做什么
  • 秦皇岛今日海港区新闻宁波搜索引擎优化seo
  • php网站开发开题报告求几个好看的关键词
  • 建网站 网站内容怎么做重庆百度关键词优化软件
  • h5网站用什么软件做江西seo推广软件
  • 做app推广被警察传唤网络优化培训要多少钱
  • 微网站制作网站制作教程
  • 做网站要写代码吗图片外链
  • 沈阳seo顾问网站关键词seo优化公司
  • 网站改名工信部需要怎么做sem是什么意思啊
  • 商城商标seo1现在怎么看不了
  • 赣州万图网络科技有限公司优化关键词可以选择哪个工具
  • 微网站预约网站开发新东方留学机构官网
  • 石佛营网站建设新闻博客软文自助推广
  • wordpress主题免刷新网站优化的方法与技巧
  • 隆基泰和 做网站火锅店营销方案
  • 注册公司流程和费用 知乎网站seo顾问
  • 房地产网站建设招商做网络销售如何找客户
  • 阳信住房和城乡建设厅网站seo推广网址
  • 廊坊微信网站建设关键词优化排名的步骤
  • 四川省人民政府副秘长有哪些安徽关键词seo