//js代码
function checkCode(num){
var typeid = $("#typeid").val();
if(typeid == ""){
alert("请选择信息类别");
return false ;
}
var i = 1;
for(i=1;i<=num;i++){ //判断每一行是否填写相关的值
var dataname = $("input[name='dataname"+i+"']").val();
var isopen=$("input:radio[name='isopen"+i+"']:checked").val();
if(dataname == ""){
alert("请输入数据名称");
return false;
}
}
return true;
} //自动新增一行
$(function(){
$( ".table1" ).on( "click",".add", function() {
var num=$(".table1").find(".clone-tr").length;
if(checkCode(num)){
ht = $(this).closest(".clone-tr").clone(true); // var num=$(".table1").find(".clone-tr").length;
if(num<20){
var check=$(ht).find(".check-box");
var inp=$(ht).find("input[type='text']");
var num2 = num + 1
$(check).eq(0).find("input[type='radio']").attr("name", 'openway'+num2);
$(check).eq(1).find("input[type='radio']").attr("name", 'isopen'+num2);
inp.val("");
inp.filter(function(){
return $(this).attr("name") == "dataname1";
}).attr("name", 'dataname'+num2); inp.filter(function(){
return $(this).attr("name") == "datacontent1";
}).attr("name", 'datacontent'+num2); inp.filter(function(){
return $(this).attr("name") == "openInterface1";
}).attr("name", 'openInterface'+num2); inp.filter(function(){
return $(this).attr("name") == "noOpenInterface1";
}).attr("name", 'noOpenInterface'+num2); $(ht).find(".operate").remove();
$(this).closest(".table1 tbody").prepend(ht); $("#numid").val(num);
$("#num2id").val(num2); //保存添加后的tr数目
}
} });
$( ".table1" ).on( "click",".delete", function() {
var num=$(".table1").find(".clone-tr").length;
if(num>1){
$(this).closest(".table1 tbody").find("tr").eq(0).remove();
} }); $("#submitid").click(submitAnswer); });
//拼json 串
function writeToJson(num){
var i = 1;
var jsonData = "[";
for(i=1;i<=num;i++){
var dataname = $("input[name='dataname"+i+"']").val();
var datacontent = $("input[name='datacontent"+i+"']").val();
var openInterface = $("input[name='openInterface"+i+"']").val();
var noOpenInterface = $("input[name='noOpenInterface"+i+"']").val();
var openway=$("input:radio[name='openway"+i+"']:checked").val();
var isopen=$("input:radio[name='isopen"+i+"']:checked").val();
if(i == num ){
jsonData += "{'dataname':'"+dataname+"','datacontent':'"+datacontent+"','openway':'"+openway+"','isopen':'"+isopen+"','openInterface':'"+openInterface+"','noOpenInterface':'"+noOpenInterface+"'}";
}else{
jsonData += "{'dataname':'"+dataname+"','datacontent':'"+datacontent+"','openway':'"+openway+"','isopen':'"+isopen+"','openInterface':'"+openInterface+"','noOpenInterface':'"+noOpenInterface+"'},";
}
}
jsonData += "]" ;
//alert("--------"+jsonData);
return jsonData ;
}
function submitAnswer(){
var num = $("#num2id").val();
if(num == ""){
num = 1;
}
if(checkCode(num)){
var cstr = writeToJson(num); // 拼 json 串
var typeid = $("#typeid").val();
$.ajax({
url: "/saveSurWXData.do",
cache: false,
type: "POST",
dataType: "json",
data: {
"content":cstr,
"typeid":typeid
},
success:function(data){
var a = data.topicId; //后台返回json到前台后处理
$("#topicId").val(a);
alert("保存成功");
window.location.reload();
},
error: function(){alert("保存失败");}
});
} }

 jsp 页面:

 <tr class="clone-tr">
<td><input type="text" class="inp" name="dataname1" placeholder="请输入"/></td>
<td><input type="text" class="inp" name="datacontent1" placeholder="请输入"/></td> <td>
<div class="check-box">
<label><input name="isopen1" type="radio" value="是" checked>是</label>
<label><input type="radio" name="isopen1" value="否">否</label>
</div>
</td>
<td>
<p class="box-b">
<input style="width: 160px;" type="text" class="inp" name="noOpenInterface1" placeholder="请输入"/>
</p>
</td>
<td class="btn-operate"><span class="operate"><a class="add">+</a><a class="delete">-</a></span> </td>
</tr>

  后台处理json 并返回值:

String jsonStr = this.getRequest().getParameter("content");
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
for(int i=0;i<jsonArray.size(); i++){
JSONObject jsonJ = jsonArray.getJSONObject(i);
String dataname = jsonJ.getString("dataname");
String datacontent = jsonJ.getString("datacontent"); String openway = jsonJ.getString("openway");
String isopen = jsonJ.getString("isopen"); String openInterface = jsonJ.getString("openInterface");
String openperiod = jsonJ.getString("noOpenInterface");
}     //返回json 到前台 JSONObject jsonObject = new JSONObject();
jsonObject.put("topicId", topicId);
this.getPrintWriter(jsonObject);   private void getPrintWriter(Object object) {
try {
out = this.getResponse().getWriter();
out.print(object);
out.close();
} catch (IOException e) {
System.out.println("error:"+e.getMessage());
}
}

 

当然还有 struts 的配置:

<action name="*" class="com.*action.*" method="*">
<result name="success" type="json"></result>
</action>

  

 

项目中多条数据保存的json实例的更多相关文章

  1. 玩转Web之Json(三)-----easy ui怎么把前台显示的dataGird中的所有数据序列化为json,返回到后台并解析

    最近做一个项目时,需要在dataGird中插入<input>,即文本输入框,当点击提交时,需要把文本框里填的数据返以及其他列的一些信息以json数组的格式返回到后台,虽然我实现了该功能,但 ...

  2. vue-cli项目中怎么mock数据

    在vue项目中, mock数据可以使用 node 的 express模块搭建服务 1. 在根目录下创建 test 目录, 用来存放模拟的 json 数据, 在 test 目录下创建模拟的数据 data ...

  3. vue cli3以上的项目中如何使用axois请求本地json文件

    首先明确一点,在vue cli3以上的版本中,存放静态资源的文件是public 我刚开始以为是和vue cli2一样需要放在static文件夹下,但是项目中没有这个文件夹,我就自己创建了一个,结果请求 ...

  4. easyui分页,编辑datagrid某条数据保存以后跳转到某一页

    参考资料:http://caizhilin2010.iteye.com/blog/1731698 问题:商品列表页面采用easyui的datagrid展示数据,编辑某行数据保存以后,要求跳转到 用户在 ...

  5. 关于项目中的DAL数据接入层架构设计

    摘要:项目中对关系型数据库的接入再寻常不过,也有海量的ORM工具可供选择,一个一般性的DAL数据接入层的结构却大同小异,这里就分享一下使用Hibernate.Spring.Hessian这三大工具对D ...

  6. Python中如何将数据存储为json格式的文件

    一.基于json模块的存储.读取数据 names_writer.py import json names = ['joker','joe','nacy','timi'] filename='names ...

  7. docker 使用mysqldump命令备份导出项目中的mysql数据

    下图为镜像重命名后的镜像名为uoj,现在要把这个镜像中的mysql导出 运行如下命令: docker exec -it uoj mysqldump -uroot -proot app_uoj233 & ...

  8. react项目中实现元素的拖动和缩放实例

    在react项目中实现此功能可借助 react-rnd 库,文档地址:https://github.com/bokuweb/react-rnd#Screenshot .下面是实例运用: import ...

  9. 获取一个表单字段中多条数据并转化为json格式

    如图需要获取下面两个li标签里面的数据,然后传给后台:而后台接收的数据格式是json的,所以需要把两个li里面的信息转化为以下格式的. {recieverName:小红,recieverPhone:1 ...

随机推荐

  1. CSS小技巧收藏

    居中对齐 很多时候我们需要把一个元素在其父级容器里水平.垂直居中对齐.以下我列出了常用的几种方法: 1.在知道子元素宽度与高度的情况下进行居中,采用位置定位:absolute + margin .pa ...

  2. HihoCoder 1121二分图一•二分图判定

    背景: 个名字,表示这两个人有一场相亲.由于姑姑年龄比较大了记性不是太好,加上相亲的人很多,所以姑姑一时也想不起来其中有些人的性别.因此她拜托我检查一下相亲表里面有没有错误的记录,即是否把两个同性安排 ...

  3. Rigging a Versatile Weapon Bone for 3ds Max

    说明:先添加weapon到点的约束,位置,方向约束都调整好了后再建立点到手,hip的父子关系,注意这个顺序 加点的方法 点设置成box的方法: http://hewiki.heroengine.com ...

  4. [Xcode 实际操作]八、网络与多线程-(25)实现ShareSdk的社会化分享功能

    目录:[Swift]Xcode实际操作 完成开发包的安装和配置之后,本文将演示社会化分享功能的具体开发步骤. 在项目导航区,打开并编辑程序代理文件[AppDelegate.swift] import ...

  5. css 所有选择器 实例与总结

    目录 什么是选择器? 选择器都有那些呢? 标签选择器 ID选择器 类选择器 后代选择器 子代选择器 组合选择器 交集选择器 相邻兄弟选择器 通用兄弟选择器 属性选择器 伪类选择器 什么是选择器? 在c ...

  6. 【OpenJ_Bailian - 2797】最短前缀(贪心)

    最短前缀 Descriptions: 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: "c", "ca&qu ...

  7. 几例实用的Shell脚本

    日常工作中,经常编写一些shell命令或脚本以完成重复性操作,本文分享了最近用到的几例shell实用脚本. 1 特殊文件名的远程拷贝 服务器之间拷贝文件经常使用scp命令,其命令格式: (1)scp ...

  8. mysql 维护添加远程主机访问

    https://www.cnblogs.com/JNUX/p/6936548.html

  9. C#中MessageBox用法大全(附效果图)<转>

    我们在程序中经常会用到MessageBox. MessageBox.Show()共有21中重载方法.现将其常见用法总结如下: 1.MessageBox.Show("Hello~~~~&quo ...

  10. springmvc 实现原理与struts2原理的区别

    spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. spring mvc是基于方法的设计,sturts2是基于类设计的. springmvc将ur ...