亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JavaBean轉json錯誤

JavaBean轉json錯誤

千萬里不及你 2019-02-25 11:07:20
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

查看完整回答
反對 回復 2019-03-01
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

你可以考慮給每個變量生成默認的get和set方法。

查看完整回答
反對 回復 2019-03-01
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

謝邀。net.sf.json印象中是比較老的一個包,建議使用jackson和gson。關鍵字可以搜索google:

  • jackson object to json

  • gson object to json

查看完整回答
反對 回復 2019-03-01
?
一只萌萌小番薯

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);

多看看文檔!希望能幫到你

查看完整回答
反對 回復 2019-03-01
  • 6 回答
  • 0 關注
  • 515 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號