json相关异常

1:  fromObject = JSONObject.fromObject("");

net.sf.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1112)
at net.sf.json.JSONObject._fromString(JSONObject.java:1337)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONObject.fromObject(JSONObject.java:156)
at cn.wwb.test.TestDemo.main(TestDemo.java:71)

因为一个字符没有,所以说0位置缺少{

2:fromObject = JSONObject.fromObject("88");
System.out.println("fromObject"+fromObject);

net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of 88
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1112)
at net.sf.json.JSONObject._fromString(JSONObject.java:1337)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONObject.fromObject(JSONObject.java:156)
at cn.wwb.test.TestDemo.main(TestDemo.java:71)

因为有字符,所以说1位置缺少{

总结以上两点是说最左边缺少{

3fromObject = JSONObject.fromObject("{}");
System.out.println("fromObject"+fromObject);
jsonArray = fromObject.getJSONArray("result");
System.out.println( "数组"+jsonArray);

net.sf.json.JSONException: JSONObject["result"] is not a JSONArray.
at net.sf.json.JSONObject.getJSONArray(JSONObject.java:2143)
at cn.wwb.test.TestDemo.main(TestDemo.java:73)

这是因为根据result拿到的不是数组

4:

json相关异常_第1张图片

net.sf.json.JSONException: A JSONObject text must begin with '{' at character 0 of 
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1112)
at net.sf.json.JSONObject._fromString(JSONObject.java:1337)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONObject.fromObject(JSONObject.java:156)
at cn.wwb.test.TestDemo.main(TestDemo.java:71)
Exception in thread "main" java.lang.NullPointerException
at cn.wwb.test.TestDemo.main(TestDemo.java:81)

如果fromObject的这块出了异常下面的就不能运行,因为111就不执行了,所以jsonArray默认就是null,就会报告空指针异常,说明将这些问题代码try--catch后下面的能够继续执行

5:

json相关异常_第2张图片

说明要通过fromObject(参数)获取到的数据出现Error问题才会报这种错误,那就在(参数)位置找错误



你可能感兴趣的:(json相关异常)