JSON格式的String 怎么转成 net.sf.json.JSONObject

jgsmile 2014-01-21 11:26:13
我有一串json格式的String 现在想给转成JSONObject
我还另写了一个方法 是对net.sf.json.JSONObject做的处理 让其变成对应的java类
但是现在怎么有String转成JSONObject完成不了 请各位帮帮忙
注:不是用org.json.simple.JSONObject里面的JSONValue.param()的方法 而是要转成net.sf.json.JSONObject对象


总之最终想实现的目的是:
我用java类(模拟器)发送了个post请求到服务器,然后返回了个JSON格式的String(可以成功拿到数据的)
现在 我想在模拟器中 还原回那个java对象 (我的想法是 先把那个String转成JSONObject 然后再由这个JSONObject转成java对象 且我已经封装好了 把JSONObject转成java对象的方法 但是其用的是net.sf.json.JSONObject里的方法)
...全文
18967 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tribeWang 2016-09-08
  • 打赏
  • 举报
回复
引用 12 楼 getter1989 的回复:
LZ怎么解决的?我的还是不行,求大神解答! 这是我的各包版本: commons-collections 3.2.1 commons-beanutils1.8.0 commons-lang 2.5 commons-logging 1.2 ezmorph 1.0.6 json-lib 2.4 JSONObject json=JSONObject.fromObject(data); 传的data是下面这个: {ltRecoveryWork=32323, iTitle=12, id=, resources=[{iMaterialCode=3, iNumber=23}, {iMaterialCode=4, iNumber=23}], iIndirectEconomic=3, iEventId=56, ltReport=23232323, iDirectEconomic=1, iDeath=2, iInjury=23, iHospitalization=3, iUnitId=2100712304} 报错是: java.lang.NoClassDefFoundError: Could not i nitialize class net.sf.json.JsonConfig] with root cause java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.JsonConfig at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
data里不是等号=,应该是冒号:
getter1989 2015-06-24
  • 打赏
  • 举报
回复
LZ怎么解决的?我的还是不行,求大神解答! 这是我的各包版本: commons-collections 3.2.1 commons-beanutils1.8.0 commons-lang 2.5 commons-logging 1.2 ezmorph 1.0.6 json-lib 2.4 JSONObject json=JSONObject.fromObject(data); 传的data是下面这个: {ltRecoveryWork=32323, iTitle=12, id=, resources=[{iMaterialCode=3, iNumber=23}, {iMaterialCode=4, iNumber=23}], iIndirectEconomic=3, iEventId=56, ltReport=23232323, iDirectEconomic=1, iDeath=2, iInjury=23, iHospitalization=3, iUnitId=2100712304} 报错是: java.lang.NoClassDefFoundError: Could not i nitialize class net.sf.json.JsonConfig] with root cause java.lang.NoClassDefFoundError: Could not initialize class net.sf.json.JsonConfig at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
善良的曾经 2015-04-28
  • 打赏
  • 举报
回复
楼主在吗 我遇到你一样的问题 你能把你的环境发给我看看么
xiaofu201111 2015-03-22
  • 打赏
  • 举报
回复
我也是,宝没有导入完!
linzisongB 2014-05-11
  • 打赏
  • 举报
回复
谢谢,受教了,我也是只引了一个包
jgsmile 2014-01-21
  • 打赏
  • 举报
回复
引用 4 楼 fangmingshijie 的回复:
commons-beanutils.jar commons-collections.jar commons-lang.jar commons-logging.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar 依赖这么点jar。
感谢fangmingshijie 问题已经解决了 之前我只是引用了一个json-lib-2.4-jdk15.jar 然后用了fromObject 报找不到类的错 我以为json-lib-2.4-jdk15.jar封装了所有的json方法 只需要这一个就行了 然后就以为这个方法不能用 现在包导全了 问题解决了 谢谢了
jgsmile 2014-01-21
  • 打赏
  • 举报
回复
引用 6 楼 zuoziji_lj 的回复:
JSONObject object = new JSONObject(str);
net.sf.json.JSONObject 好像不支持这个构造方法呐
卡卡吉利 2014-01-21
  • 打赏
  • 举报
回复
JSONObject object = new JSONObject(str);
jgsmile 2014-01-21
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
JSONObject.fromObject(str);


引用 2 楼 ghostkngiht 的回复:
JSONObject.fromObject(str);


是我传的String的JSON格式有问题吗?
  • 打赏
  • 举报
回复
commons-beanutils.jar commons-collections.jar commons-lang.jar commons-logging.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15.jar 依赖这么点jar。
jgsmile 2014-01-21
  • 打赏
  • 举报
回复
我试过好多次这个方法 都是报错

ghostkngiht 2014-01-21
  • 打赏
  • 举报
回复
JSONObject.fromObject(str);
  • 打赏
  • 举报
回复
JSONObject.fromObject(str);
找了很久 的 终于搞好了 JSON数据以来包全部的jar包 解压后放在项目下就可以了 下面付源代码 import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Json { public static void main(String[] args) { Json j = new Json(); j.bean2json(); } public void arr2json() { boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); System.out.println(jsonArray); // prints [true,false,true] } public void list2json() { List list = new ArrayList(); list.add("first"); list.add("second"); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); // prints ["first","second"] } public void createJson() { JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']"); System.out.println(jsonArray); // prints ["json","is","easy"] } public void map2json() { Map map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject json = JSONObject.fromObject(map); System.out.println(json); // prints // ["name":"json","bool":true,"int":1,"arr":["a","b"],"func":function(i){ // return this.arr[i]; }] } public void bean2json() { JSONObject jsonObject = JSONObject.fromObject(new MyBean()); System.out.println(jsonObject); } public void json2bean() { String json = "{name=\"json2\",func1:true,pojoId:1,func2:function(a){ return a; },options:['1','2']}"; JSONObject jb = JSONObject.fromString(json); JSONObject.toBean(jb, MyBean.class); System.out.println(); } } 操作的bean: import net.sf.json.JSONFunction; public class MyBean { private String name = "json"; private int pojoId = 1; // private char[] options = new char[] { 'a', 'f' }; private String func1 = "function(i){ return this.options[i]; }"; private JSONFunction func2 = new JSONFunction(new String[] { "i" }, "return this.options[i];"); // getters & setters ...... }

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧