自身一對(duì)多關(guān)聯(lián)模型的JSON轉(zhuǎn)換

                 最近又遇到了一個(gè)問(wèn)題, 這里有一個(gè)一對(duì)多自身關(guān)聯(lián)的模型
           1Class Tree
           2{
           3   private String id;
           4   private String name;
           5   private Set<tree> children = new HashSet<Tree>();
           6   public void setId(String id)
           7   {
           8         this.id = id;
           9   }

          10   public String getId()
          11   {
          12         return this.id;
          13   }

          14    
          15}
          然后我用JSON把這個(gè)模型轉(zhuǎn)換成string的時(shí)候會(huì)報(bào)錯(cuò):will throw a JSONException if a cycle is found.后來(lái)去查了下JSON的API發(fā)現(xiàn)是我的JSON轉(zhuǎn)換策略有問(wèn)題。原文是這樣的:
          public abstract class CycleDetectionStrategy
          extends Object
          
          

          Base class for cycle detection in a hierarchy.
          The JSON spec forbides cycles in a hierarchy and most parsers will raise and error when a cycle is detected. This class defines a contract for handling those cycles and two base implementations:

          • STRICT - will throw a JSONException if a cycle is found.
          • LENIENT - will return an empty array or null object if a cycle is found.

          說(shuō)的很清楚了所以必須做點(diǎn)修改像這樣:
           1public class TreeAction extends BaseActionSupport{
           2    private static final CycleDetectionStrategy CycleDetectionStrategy = null;
           3    public String getAllTree()
           4    {
           5        JsonConfig jsonConfig=new JsonConfig();
           6        jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
           7        tree = regionService.getTreeChildren("402881821a7fbf10011a7fc2d3ae0001");
           8        JSONObject array = JSONObject.fromObject(tree,jsonConfig);
           9        System.out.println("$$$$$$$$$$"+array.toString()+"$$$$$$$$$$$$$$");
          10        return SUCCESS;
          11    }

          12}

          這樣修改以后轉(zhuǎn)換就正常啦~~~。呵呵,HIBERNATE還有好多問(wèn)題我沒(méi)解決煩惱中。。。。。。


          posted on 2008-06-20 22:59 Cloud kensin 閱讀(1089) 評(píng)論(0)  編輯  收藏 所屬分類: Java

          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(4)

          隨筆分類

          相冊(cè)

          相冊(cè)

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 南川市| 南充市| 墨玉县| 松溪县| 来安县| 威海市| 堆龙德庆县| 逊克县| 黑河市| 贞丰县| 昆山市| 河南省| 永顺县| 施秉县| 仲巴县| 黑山县| 宝清县| 四平市| 孙吴县| 通化县| 麦盖提县| 霸州市| 云南省| 青海省| 汝州市| 白城市| 龙里县| 松原市| 聂拉木县| 通海县| 同江市| 阿拉尔市| 从化市| 乾安县| 南乐县| 渝北区| 香港 | 永清县| 赞皇县| 凌云县| 黑龙江省|