The method fromObject(String) is undefined for the type JSONObject
来源:互联网 发布:尚长荣唱的怎么样知乎 编辑:程序博客网 时间:2024/06/06 13:10
文章转载:http://my.oschina.net/wangwu91/blog/340721
如果有版权问题,请联系我。
原文标题:net.sf.json.JSONObject 和org.json.JSONObject 的差别
其实,出现fromObject没有被定义,是因为json有两个包,就是net.sf.json和org.json。在使用json的过程中,会有包的导入。
看你选择导入的包,如果选择的是org.json.json这个包,就会出现undefined。
下面是转载过来的文章。关于两个包之间的区别。
net.sf.json.JSONObject 和org.json.JSONObject 的差别。
一、创建json对象
String str = "{\"code\":\"0000\", \"msg\":{\"availableBalance\":31503079.02}}
org.json.JSONObject:
JSONObject json = new JSONObject(str);
net.sf.json.JSONObject:
JSONObject json = JSONObject.fromObject(str);
net.sf.json.jsonobject 没有 new JSONObject(String)的构造方法
二、解析json
第一种直接用json对象.getXXX();方法获取
net.sf.json.JSONObject: 没有严格要求获取字段的类型跟getXXX()的类型一样
org.json.JSONObject:获取的字段类型必须跟getXXX()的类型一样
e.g.
JSONObject msgObj = json.getJSONObject("msg");String availableBalance = msgObj.getString("availableBalance");
{\"availableBalance\":\"31503079.02\"}}";
org.json.JSONObject 就会报错,可以msgObj.getDouble(“availableBalance”);也不会丢精度;
net.sf.json.JSONObject正确,但是精度会丢失,如果 String str = "{\"code\":\"0000\", \"msg\":
就不会丢失精度。
第二中json对象直接转变实体对象
public class BalanceDto {private String availableBalance; public String getAvailableBalance() { return availableBalance; } public void setAvailableBalance(String availableBalance) { this.availableBalance = availableBalance; } public String toString(){ return "availableBalance"+availableBalance; }}
org.json.JSONObject:
BalanceDto alanceDto = (BalanceDto) JSONObject.stringToValue(msgObj);
这个句话编译通过,但是运行会报错,原因是BalanceDto 类中availableBalance 的类型跟json中的“availableBalance ”类型不同意
net.sf.json.JSONObject:
String msg = json.getString("msg");BalanceDto alanceDto = (BalanceDto) JSONObject.toBean(msg, new BalanceDto().getClass());
三、从json中获取数组
JSONArray subArray = json.getJSONArray(“msg”);
net.sf.json.JSONObject:
int leng = subArray.size();
org.json.JSONObject:
int leng = subArray.length();
json net.sf.json的需要的包:
下载地址:http://download.csdn.net/detail/shaoqianting/8698183
- The method fromObject(String) is undefined for the type JSONObject
- The method isEmpty() is undefined for the type String/String
- The method setCharacterEncoding(String) is undefined for the type HttpServletResponse
- The method setCharacterEncoding(String) is undefined for the type HttpServletResponse
- The method isEmpty() is undefined for the type String
- The method setCharacterEncoding(String) is undefined for the type HttpServle
- he method isEmpty() is undefined for the type String/String
- the method d(string string) is undefined for the type log
- 开发中碰到的问题:The method getSystemService(String) is undefined for the type CourseFragment
- jfinal报错The method renderText(String) is undefined for the type HelloController
- 运行maven项目报错:The method queryForInt(String) is undefined for the type JdbcTemplate
- jeeplus 使用:The method setShi(String) is undefined for the type addRow_tag
- 错误提示:The method parseInt(String) is undefined for the type Loginsample
- Pb(100) The method getType() is undefined for the type new TypeToken<List<String>>(){}
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory!
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
- The method getJspApplicationContext(ServletContext) is undefined for the type
- 在 SQL Server 中从完整路径提取文件名(sql 玩转文件路径)
- eclipse常用快捷键
- 动画整理(jquery+css3+canvas+angularjs+js )
- ansible学习之--简单学习笔记1
- android RatingBar 自定义
- The method fromObject(String) is undefined for the type JSONObject
- Android主线程的消息系统(Handler\Looper)
- iOS 设置整理
- java知识2015-07-10
- Unity3D之如何将包大小减少到极致
- ssh 连接错误
- ZOJ-3838-Infusion Altar
- C#实现麦克风采集与播放
- vim 命令(全)