解析Json 出现——java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
2016-06-13 10:20
567 查看
在后台写了一个调用百度api接口,将GPS坐标转换成百度坐标的方法,
获得返回的一个json串,
jsonStr = "{\"status\":0,\"result\":[{\"x\":123.98636285887,\"y\":42.457133104428},{\"x\":123.98739247735,\"y\":42.462356027015}]}";
用main方法测试解析这个json串
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
JSONObject jsonobj = JSONObject.fromObject(jsonStr);
JSONArray objs = jsonobj.getJSONArray("result");
String[] db = new String[4];
JSONObject jo = objs.optJSONObject(0);
String x = jo.getString("x");
String y = jo.getString("y");
db[0]=x;
db[1]=y;
JSONObject jo1 = objs.optJSONObject(1);
String x1 = jo1.getString("x");
String y1 = jo1.getString("y");
db[2]=x1;
db[3]=y1;
完全没有问题。
但是在Jfinal 框架下,用web,从页面点击进入Controller->调用这个转换方法,解析的过程中
在JSONObject jsonobj = JSONObject.fromObject(jsonStr); 处报错:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
很遗憾,不知道问题是什么,
没办法为了赶工,就用了阿里巴巴的fastjson,运行没有问题
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
代码片段,
JSONObject jsonobj = JSONObject.parseObject(jsonStr);
JSONArray objs = jsonobj.getJSONArray("result");
String[] db = new String[4];
问题解决。
虽然有些遗憾未能找到原因。有时间再琢磨琢磨
获得返回的一个json串,
jsonStr = "{\"status\":0,\"result\":[{\"x\":123.98636285887,\"y\":42.457133104428},{\"x\":123.98739247735,\"y\":42.462356027015}]}";
用main方法测试解析这个json串
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
JSONObject jsonobj = JSONObject.fromObject(jsonStr);
JSONArray objs = jsonobj.getJSONArray("result");
String[] db = new String[4];
JSONObject jo = objs.optJSONObject(0);
String x = jo.getString("x");
String y = jo.getString("y");
db[0]=x;
db[1]=y;
JSONObject jo1 = objs.optJSONObject(1);
String x1 = jo1.getString("x");
String y1 = jo1.getString("y");
db[2]=x1;
db[3]=y1;
完全没有问题。
但是在Jfinal 框架下,用web,从页面点击进入Controller->调用这个转换方法,解析的过程中
在JSONObject jsonobj = JSONObject.fromObject(jsonStr); 处报错:
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
很遗憾,不知道问题是什么,
没办法为了赶工,就用了阿里巴巴的fastjson,运行没有问题
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
代码片段,
JSONObject jsonobj = JSONObject.parseObject(jsonStr);
JSONArray objs = jsonobj.getJSONArray("result");
String[] db = new String[4];
问题解决。
虽然有些遗憾未能找到原因。有时间再琢磨琢磨
相关文章推荐
- Eclipse添加builder自动执行bat复制脚本
- Java transient关键字
- eclipse 代码格式化
- java反射抄的例子
- eclipse libs 和 buildpath区别
- eclipse 使用Maven deploy命令部署构建到Nexus上
- Struts2学习笔记(9)-Result配置全局结果集
- 如何在eclipse中使用反编译插件
- 《疯狂Java讲义(第3版)》.(李刚)——面向对象(上)
- volatile 变量
- 【Java多线程】同步辅助类CyclicBarrier
- Eclipse Android项目开发完成以后就要将android项目文件打包成apk文件
- Struts2学习笔记(8)-Result常用类型
- Eclipse Egit 安装
- [置顶] Spring Boot实践折腾记汇总
- [LeetCode][12]Integer to Roman解析 int转罗马字符时间复杂度为常数的实现-Java实现
- 解决Eclipse交叉编译环境下出现Symbol"NULL" could not be resolved
- Spring mvc web 配置 简单例子 (创建Spring mvc web项目)
- 从MyEclipse2014到MyEclispe10
- nutch1.7在eclipse中部署