结论:org.json.JSONObject的getString如果取不到对应的key会抛出异常,optString则不会

/**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary, or throws if no such mapping exists.
*
* @throws JSONException if no such mapping exists.
*/
public String getString(String name) throws JSONException {
Object object = get(name);
String result = JSON.toString(object);
if (result == null) {
throw JSON.typeMismatch(name, object, "String");
}
return result;
} /**
* Returns the value mapped by {@code name} if it exists, coercing it if
* necessary, or the empty string if no such mapping exists.
*/
public String optString(String name) {
return optString(name, "");
}

org.json.JSONObject的getString和optString使用注意事项的更多相关文章

  1. jsonobject 遍历 org.json.JSONObject

    import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public static  ...

  2. net.sf.json.JSONObject 和org.json.JSONObject 的差别

    http://my.oschina.net/wangwu91/blog/340721 net.sf.json.JSONObject 和org.json.JSONObject  的差别. 一.创建jso ...

  3. java.lang.ClassCastException: net.sf.json.JSONNull cannot be cast to net.sf.json.JSONObject的解决方法

    报错情况已经说明了,在百度查了好几个解决方法,这里总结一下: 首先:加一个判断是否为空,再做操作 // 得到json串 String jsonString = UtilPOSTGET.UPost(FO ...

  4. net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject

    文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...

  5. [转]json+JSONObject+JSONArray 结合使用

    JSONObject与JSONArray的区别简述: 区别在于JSONObject是一个{}包裹起来的一个对象(Object),而JSONArray则是[]包裹起来的一个数组(Array),说白点就是 ...

  6. Jmeter not found in class'org.json.JSONObject 问题

    前景:公司有银行的项目要进行压测,但是接口有近视RSA加密,需发送签名,只能使用java编写原生接口脚本打包成jar使用BeanShell Sampler去调用发送请求.在使用的过程中遇到了如下问题. ...

  7. 使用JSONObject遇到的问题,java.lang.NoClassDefFoundError: net/sf/json/JSONObject

    先是报 java.lang.NoClassDefFoundError: net/sf/json/JSONObject 这个错误, 打开项目属性找到java build path中的libaries,找 ...

  8. Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...

  9. sqoop执行job报错(org/json/JSONObject)

    NoClassDefFoundError: org/json/JSONObject: [root@hadoop2 ~]# sqoop job --create myjob7 --  import -- ...

随机推荐

  1. Codeforces 1093D Beautiful Graph(二分图染色+计数)

    题目链接:Beautiful Graph 题意:给定一张无向无权图,每个顶点可以赋值1,2,3,现要求相邻节点一奇一偶,求符合要求的图的个数. 题解:由于一奇一偶,需二分图判定,染色.判定失败,直接输 ...

  2. uploadify多文件上传实例--C#

    下载uploadify文件 http://www.uploadify.com/ HTML(视图) <html lang="zh-cn"> <head> &l ...

  3. idea代码出现Usage of API documented as @since 1.8+ less... (Ctrl+F1)

    问题: Usage of API documented as @since 1.8+ less... (Ctrl+F1) This inspection finds all usages of met ...

  4. js重点--this关键字

    推荐博客:https://www.cnblogs.com/huaxili/p/5407559.html this是JavaScript的一个关键字,表示的不是对象本身,而是指被调用的上文. 主要用于以 ...

  5. 从线性模型(linear model)衍生出的机器学习分类器(classifier)

    1. 线性模型简介 0x1:线性模型的现实意义 在一个理想的连续世界中,任何非线性的东西都可以被线性的东西来拟合(参考Taylor Expansion公式),所以理论上线性模型可以模拟物理世界中的绝大 ...

  6. 网络协议 HTTP入门学习

    概述 Web 的诞生,源于三大技术的诞生,它们都是当年 Web 之父 Tim Berners-Lee 自己 开发的,世界上第一个网站诞生的时间是 1991 年,三大技术的诞生也就是在此之前的不久: 1 ...

  7. count

    select deptno as 部门,count(*) as 人数from emp group by deptno;  --统计各个部门的人数

  8. C#调用Bartender打印

    BarTender是一款优秀的条形码打印软件,可以支持很多种类型的条形码设计和打印,具体大家可参考他的官网(http://www.seagullscientific.com/aspx/products ...

  9. 【gitlab】gitlab快速部署教程

    gitlab快速部署教程 部署环境 Ubuntu 16.04(亲测可用) 开始部署 安装依赖 sudo apt-get install curl openssh-server ca-certifica ...

  10. JAVA进阶19

    1.冒泡排序 package cn.zh.abstrac; import java.util.Arrays; //冒泡排序 public class Demo019 { public static v ...