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>();}
轉(zhuǎn)化代碼: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貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
這個(gè)問題,我之前也遇到過:對(duì)象嵌套對(duì)象的情況,不能直接用 JSONObject.fromObject(page) 這方法。
我當(dāng)時(shí)處理的辦法很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")){ // 嵌套的對(duì)象
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")){ // 嵌套的對(duì)象
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 對(duì)象嵌套 轉(zhuǎn)json

江戶川亂折騰
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊
謝邀。net.sf.json
印象中是比較老的一個(gè)包,建議使用jackson和gson。關(guān)鍵字可以搜索google:
jackson object to json
gson object to json

一只萌萌小番薯
TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
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);
多看看文檔!希望能幫到你
添加回答
舉報(bào)
0/150
提交
取消