jsp-js-java:json数据传值

来源:互联网 发布:简单c语言程序代码 编辑:程序博客网 时间:2024/05/24 03:54

jsp:

                   <table class="posttable">
                        <colgroup>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                            <col style="min-width: 70px; width: 10%;"/>
                        </colgroup>
                        <tbody>
                            <tr>
                                <th colspan="5">(1)</th>
                                <th class="centerth"><div id="option1_1" data-option-no="1"  class="optionNormal"><b>1</b></div></th>
                                <th class="centerth"><div id="option1_2"  data-option-no="1" class="optionNormal"><b>2</b></div></th>
                                <th class="centerth"><div id="option1_3"  data-option-no="1" class="optionNormal"><b>3</b></div></th>
                                <th class="centerth"><div id="option1_4"  data-option-no="1" class="optionNormal"><b>4</b></div></th>
                                <th class="centerth"><div id="option1_5"  data-option-no="1" class="optionNormal"><b>5</b></div></th>
                            </tr>
                            <tr>
                                <th  colspan="5">(2))</th>
                                <th class="centerth"><div id="option2_1"  data-option-no="2" class="optionNormal"><b>1</b></div></th>
                                <th class="centerth"><div id="option2_2"  data-option-no="2" class="optionNormal"><b>2</b></div></th>
                                <th class="centerth"><div id="option2_3"  data-option-no="2" class="optionNormal"><b>3</b></div></th>
                                <th class="centerth"><div id="option2_4"  data-option-no="2" class="optionNormal"><b>4</b></div></th>
                                <th class="centerth"><div id="option2_5"  data-option-no="2" class="optionNormal"><b>5</b></div></th>
                            </tr>
                            <tr>
                                <th  colspan="5">(3)</th>
                                <th class="centerth"><div id="option3_1"  data-option-no="3" class="optionNormal"><b>1</b></div></th>
                                <th class="centerth"><div id="option3_2"  data-option-no="3" class="optionNormal"><b>2</b></div></th>
                                <th class="centerth"><div id="option3_3"  data-option-no="3" class="optionNormal"><b>3</b></div></th>
                                <th class="centerth"><div id="option3_4"  data-option-no="3" class="optionNormal"><b>4</b></div></th>
                                <th class="centerth"><div id="option3_5"  data-option-no="3" class="optionNormal"><b>5</b></div></th>
                            </tr>
                        </tbody>
                    </table>   



js:

      var optionDatas = getOptionData();   //optionDatas= [[object Object],[object Object],[object Object]]

     param : {
                    optionDatas : util.Obj2str(optionDatas)   //    util.Obj2str(optionDatas) =[{"optionNo":1,"score":"3"},{"optionNo":2,"score":"1"},{"optionNo":3,"score":"4"}]
     },

   function getOptionData(){
        var optionDatas = [];
        $("th div[id^='option']").each(function(){
            var optionData = {};
            if($(this).hasClass("optionSelect")){
                optionData['optionNo'] = $(this).data('optionNo');
                var ids = this.id.split('_');
                optionData['score'] = ids[1];
                optionDatas.push(optionData);
            }
        });
        return optionDatas;
    } 


该方法为Util.js的方法

/* 对象转成json/json数组 */
    function Obj2str(o)
    {
        if (o == undefined)
        {
            return "";
        }
        var r = [];
        if (typeof o == "string")
            return "\"" + o.replace(/([\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
        if (typeof o == "object")
        {
            if (!o.sort)
            {
                for ( var i in o)
                    r.push("\"" + i + "\":" + Obj2str(o[i]));
                if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString))
                {
                    r.push("toString:" + o.toString.toString());
                }
                r = "{" + r.join() + "}";
            } else
            {
                for ( var i = 0; i < o.length; i++)
                    r.push(Obj2str(o[i]));
                r = "[" + r.join() + "]";
            }
            return r;
        }
        return o.toString().replace(/\"\:/g, '":""');
    }


java:

       // String optionDatas传入后台为字符串类型 ='[{"optionNo":1,"score":"3"},{"optionNo":2,"score":"1"},{"optionNo":3,"score":"4"}]';

       if (StringUtil.isNotEmpty(optionDatas)) {
            List<EchOption> options = json2Obj(optionDatas);
        }


     /**
     * json数组转成List
     *
     * @param jsonArrayStr
     * @return
     */
private static List<EchOption> json2Obj(String jsonArrayStr) {
   JsonConfig jsonConfig = new JsonConfig();
   JSONArray jsonObjects = JSONArray.fromObject(jsonArrayStr, jsonConfig);
   List<EchOption> resultList = new ArrayList<EchOption>();
  for (int i = 0; i < jsonObjects.size(); i++) {
      JSONObject jsonObj = (JSONObject) jsonObjects.get(i); //  jsonObj ={"optionNo":1,"score":"3"}
      EchOption option = (EchOption) JSONObject.toBean(jsonObj, EchOption.class); //一条表记录  private Integer EchOption.optionNo=1; private Integer EchOption.score=3;
.     resultList.add(option);
  }
     return resultList;

}


               

原创粉丝点击