我的Josn解析器很长,必须创建一个新类来解决该问题,然后只需在每个方法中添加1条额外的行并重命名当前的JSONObject属性名称,因此所有其他调用都引用我的新类而不是JSONObject。

public static ArrayList readNews(String json) {

if (json != null) {

ArrayList res = new ArrayList<>();

try {

JSONArray jsonArray = new JSONArray(json);

for (int i = 0; i < jsonArray.length(); i++) {

//before JSONObject jo = jsonArray.getJSONObject(i);

JSONObject joClassic = jsonArray.getJSONObject(i);

//facade

FixJsonObject jo = new FixJsonObject(joClassic);

PieceOfNews pn = new PieceOfNews();

pn.setId(jo.getInt("id"));

pn.setImageUrl(jo.getString("imageURL"));

pn.setText(jo.getString("text"));

pn.setTitle(jo.getString("title"));

pn.setDate(jo.getLong("mills"));

res.add(pn);

}

return res;

} catch (JSONException e) {

e.printStackTrace();

}

}

return null;

}

这是我的类,其中包含我需要的方法,您可以添加更多

public class FixJsonObject {

private JSONObject jsonObject;

public FixJsonObject(JSONObject jsonObject) {

this.jsonObject = jsonObject;

}

public String optString(String key, String defaultValue) {

if (jsonObject.isNull(key)) {

return null;

} else {

return jsonObject.optString(key, defaultValue);

}

}

public String optString(String key) {

return optString(key, null);

}

public int optInt(String key) {

if (jsonObject.isNull(key)) {

return 0;

} else {

return jsonObject.optInt(key, 0);

}

}

public double optDouble(String key) {

return optDouble(key, 0);

}

public double optDouble(String key, double defaultValue) {

if (jsonObject.isNull(key)) {

return 0;

} else {

return jsonObject.optDouble(key, defaultValue);

}

}

public boolean optBoolean(String key, boolean defaultValue) {

if (jsonObject.isNull(key)) {

return false;

} else {

return jsonObject.optBoolean(key, defaultValue);

}

}

public long optLong(String key) {

if (jsonObject.isNull(key)) {

return 0;

} else {

return jsonObject.optLong(key, 0);

}

}

public long getLong(String key) {

return optLong(key);

}

public String getString(String key) {

return optString(key);

}

public int getInt(String key) {

return optInt(key);

}

public double getDouble(String key) {

return optDouble(key);

}

public JSONArray getJSONArray(String key) {

if (jsonObject.isNull(key)) {

return null;

} else {

return jsonObject.optJSONArray(key);

}

}

}

java json返回null_java-JSON jsonObject.optString()返回字符串“ null”相关推荐

  1. java e.getmessage() null_Java e.getMessage 错误信息为null

    解决方案:用e.printStackTrace() e.getMessage()为null也很正常,NullPointException的getMessage()就为null 我在抛出Exceptio ...

  2. java文件流null_JAVA 获取资源文件对象为NULL

    今天,写一个添加背景音乐的方法时,在导入当前文件夹下的音乐时中始终出现,以下的异常,Exception in thread "main" java.lang.NullPointer ...

  3. java 垃圾回收 null_java方法中把对象置null,到底能不能加速垃圾回收

    今天逛脉脉,看见匿名区有人说java中把对做置null,这种做法很菜,不能加速垃圾回收,但是我看到就觉得呵呵了,我是觉得可以加速置null对象回收的. 测试的过程中,费劲的是要指定一个合理的测试堆大小 ...

  4. java中map转为json数据_Java技术-将java中Map类型数据转化为json数据并以Ajax形式返回...

    Java技术-将java中Map类型数据转化为json数据并以Ajax形式返回html 1.自定义工具类(简单易用)-下面是我写的一个简单的工具类前端 package com.test.util; i ...

  5. java http 返回json_java发起http请求获取返回的Json对象方法

    话不多说,先看代码! /** * Created by david on 2017-7-5. */ import com.google.gson.JsonObject; import com.goog ...

  6. java百度天气接口解析_Java调用天气接口(百度天气)解析返回的JSON数据

    简介:本文详细讲述了通过Java调用百度天气接口的方法,取得返回的JSON格式的数据,并且通过第三方包解析JSON格式的数据. 通过百度天气API调用网络编程接口接收返回的JSON格式的数据. 关于百 ...

  7. 如何用java语法解析接口返回的json串?

    起因:做接口测试的时候经常需要对接口返回的响应数据(一般都是json字符串格式)做解析 下面是一个接口响应数据的例子: 下面是对上面接口返回的json格式字符串的解析 Object dataObj=J ...

  8. java 调用webapi json_java通过url调用web api并接收其返回的json

    java通过url调用webapi并接收其返回的json数据,但现在结果总是:{"result":4,"data":{}}(未认证:),帮助文档如下:API使用 ...

  9. Java框架-SpringMVC的应用(json数据交互、控制器方法返回值、文件上传)

    1. 搭建SpringMVC开发环境 1.1 创建项目,添加依赖 <?xml version="1.0" encoding="UTF-8"?> &l ...

最新文章

  1. 中国新冠研究登上Science封面,全球首次揭示新冠病毒人体蛋白受体结构
  2. DevExpress右键菜单使用 zt
  3. Double Free浅析
  4. php 接口继承,详细对比php中类继承和接口继承
  5. Hive的基本操作-自定义函数
  6. PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件
  7. docker安装rocketmq你学会了吗
  8. crond定时任务详细分析
  9. 点击图片放大至原始图片大小
  10. LeetCode 994. 腐烂的橘子(图的BFS)
  11. 洛谷 P3387 【模板】缩点
  12. 一个程序员的书法学习之路-法帖篇
  13. 哈理工OJ 1795 发现王国之旅(最小生成树)
  14. MyBatis拦截器实现SQL打印
  15. 解决swift实现的websocket与后台通信问题:websocket is disconnected: masked and rev data is not currently supported
  16. 现代 C++ 编译时 结构体字段反射
  17. 芯片封装测试流程详解,一文带你了解清楚
  18. PDF打开密码忘记了,怎么找回?
  19. 大数据如何简化业务运营
  20. 步进电机28BYJ-48

热门文章

  1. Oracle体系结构四(学习笔记)
  2. Android使用的设计模式2——策略模式
  3. 一图看懂边缘计算整体架构
  4. 中台到底是什么?传统企业怎样建设中台?
  5. 详解7类Python运算符及代码举例
  6. 清华大学张文增教授110页PPT讲专利申请、检索与解读
  7. 你尝试登录的服务器语言不通,七骑士国内服务器正式上线后,各种登录问题解决方案汇总...
  8. 看完师兄的代码笔记,我失眠了
  9. 50道Java集合经典面试题
  10. 聊聊领域分析与业务建模