Document类:public class DocBean {private String docId;private String docName;private String typeId;private String typeName;private String content;private String docTime;private String views;private String cover;}
page类public class Page {private int pageNo;private int totalItem;private int pageSize;private String typeId;private List<DocBean> doc=new ArrayList<DocBean>();}
转化代码:JSONObject page_json=JSONObject.fromObject(page);
error:net.sf.json.JSONException: There is a cycle in the hierarchy!
at net.sf.json.util.CycleDetectionStrategy$StrictCycleDetectionStrategy.handleRepeatedReferenceAsObject(CycleDetectionStrategy.java:97)
at net.sf.json.JSONObject._fromBean(JSONObject.java:833)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.AbstractJSON._processValue(AbstractJSON.java:265)
at net.sf.json.JSONObject._processValue(JSONObject.java:2808)
at net.sf.json.JSONObject.processValue(JSONObject.java:2874)
at net.sf.json.JSONObject.setInternal(JSONObject.java:2889)
at net.sf.json.JSONObject.setValue(JSONObject.java:1577)
at net.sf.json.JSONObject._fromBean(JSONObject.java:934)
at net.sf.json.JSONObject.fromObject(JSONObject.java:168)
at net.sf.json.JSONObject.fromObject(JSONObject.java:130)
at hyit.dataBind.DocBind.initDocList(DocBind.java:124)
at hyit.dataBind.DocBind.test01(DocBind.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
6 回答
ITMISS
TA贡献1871条经验 获得超8个赞
这个问题,我之前也遇到过:对象嵌套对象的情况,不能直接用 JSONObject.fromObject(page) 这方法。
我当时处理的办法很low。代码仅供参考:
public static void fromJson(String jsonStr) throws Exception{
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
Object[] objs = jsonArray.toArray();
for (Object object : objs) {
JSONObject jsonObject = JSONObject.fromObject(object);
if(jsonObject.has("images")){ // 嵌套的对象
String imgString = jsonObject.getString("images");
JSONArray imgArray = JSONArray.fromObject(imgString);
Object[] imgObjs = imgArray.toArray();
for (Object object2 : imgObjs) {
JSONObject jsonObject2 = JSONObject.fromObject(object2);
System.out.println(jsonObject2.getString("height")+"\t"+jsonObject2.getString("url"));
}
}
if(jsonObject.has("countrys")){ // 嵌套的对象
String countrysString = jsonObject.getString("countrys");
List<String> stringList = binder.getMapper().readValue(countrysString, List.class);
System.out.println("String List:");
for (String element : stringList) {
System.out.print(element+"\t");
}
}
System.out.println(jsonObject.getString("offerId")+"\t"+jsonObject.getString("name"));
}
}
你也可以选择百度:java 对象嵌套 转json
江户川乱折腾
TA贡献1851条经验 获得超5个赞
谢邀。net.sf.json
印象中是比较老的一个包,建议使用jackson和gson。关键字可以搜索google:
jackson object to json
gson object to json
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(JsonStr);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("selectInfo", SelectPropsDto.class);
classMap.put("checkBoxInfo", CheckPropsDto.class);
classMap.put("radioInfo", RadioPropsDto.class);
GoodsDto goods = (GoodsDto) net.sf.json.JSONObject.toBean(jsonObject, GoodsDto .class,classMap);
多看看文档!希望能帮到你
添加回答
举报
0/150
提交
取消