json 使用中代码复制
2012-08-23 15:23
169 查看
js中
action中
主要用了类型转换用的
宝贝网址:
数组的使用 var array = new Array(); var obj = {}; obj.number=number; array.push(obj); if(r){ var parameter = getFormParm("#bodyDiv :input"); parameter.array = array; var jsonStr=JSON.stringify(parameter); $.ajax({ type : "post", dataType : "json", data : { jsonStr : jsonStr }, url : "recipe.do?method=save", success : function(data) { $.messager.alert("提示", "操作成功"); $("#companyNo").val(''); $("#nhc01_editInfoDiv :input,textarea").val(''); $("#seridNumber").val(data.serid); $("#versions").val(data.versions); $("#receptumDiv").html('<fieldset><table id="drugInfo" style="width: 900px;font-size: 12px;" border="0" align="center"><tr align="center"><th>药名</th><th>数量</th><th>单价</th><th>小计</th><th>用法</th><th>操作</th></tr></table></fieldset>'); } }); }
action中
public @ResponseBody SeridNumber recipeSave(HttpServletRequest request, String jsonStr){ JSONObject jsonObj = new JSONObject(jsonStr); Recipe recipe = new Recipe(); recipe.setId(jsonObj.optString("id")); setValueFromJson(jsonObj, recipe);// JSON类型转换成 对象 JSONArray jsonArray = jsonObj.optJSONArray("array"); // 获得前面传过来的array 变量 /** * 保存单据下面的药品 * */ for (int i = 0; i < jsonArray.length(); i++) { JSONObject obj = jsonArray.optJSONObject(i); if (obj.length() == 0) { continue; } String drugName = obj.optString("drugName"); String numbers = obj.optString("number"); String unitPrice = obj.optString("unitPrice"); String subtotal = obj.optString("subtotal"); String usage = obj.optString("usage"); RecipeMedical recipeMedical = new RecipeMedical(recipeId, drugName, numbers, unitPrice, subtotal, usage); recipeMedical.setDelFlag("0"); recipeMedicalBO.saveRecipeMedical(recipeMedical); } } //将JSON转换成对像 private Object setValueFromJson(JSONObject jsonObj, Object object) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ParseException { if (jsonObj == null || object == null || jsonObj.length() == 0) { return null; } Method[] methods = object.getClass().getMethods(); for (Method m : methods) { String mName = m.getName(); if (!mName.startsWith("set")) { continue; } String firstLetter = mName.substring(3, 4); String otherLetter = mName.substring(4); String name = firstLetter.toLowerCase() + otherLetter; String value = jsonObj.optString(name); // 从JSON中解析出数据 if (value.trim().equals("")) { continue; } Class[] parasClass = m.getParameterTypes(); if (parasClass.length != 1) { continue; } // 判断方法参数类型 String paraType = parasClass[0].getName(); if (paraType.equals("java.lang.String")) { m.invoke(object, value); } else if (paraType.equals("java.lang.Integer")) { m.invoke(object, Integer.parseInt(value)); } else if (paraType.equals("java.math.Bigdecimal")) { m.invoke(object, new BigDecimal(value)); } } return object; }
主要用了类型转换用的
宝贝网址:
相关文章推荐
- MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
- jQuery中使用Ajax获取JSON格式数据示例代码
- 记录一段使用socket 回复的http json的请求代码
- 使用代码写成json串
- py3 使用TCP SOCKET 发送json字符串给服务器测试例程代码
- [Android | 小代码] 天气预报 简易Demo json解析数组+对象 Gson+Volley简单使用
- jquery和json使用代码
- Android使用lottie加载json动画的示例代码
- 复制并正确使用csdn文章中使用“插入代码”插入的代码
- Unity3d 通过json文件使用C#脚本代码生成多个相机
- Vue-cli 使用json server在本地模拟请求数据的示例代码
- MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号
- php语言中使用json的技巧及json的实现代码详解
- 使用 ServiceStack.Text 序列化 json的实现代码【转】
- 使用clipboard.js实现复制功能的示例代码
- JAVA功能代码《12》----使用NIO快速复制文件
- 分享两个PB移植到.NET的类,支持把PB代码复制到.NET里使用(1)
- swift:使用NSJSONSerialization解析本地json数据文件(代码详细注释)
- 如何使用ABAP代码反序列化JSON字符串成ABAP结构
- jQuery 中使用JSON的实现代码