Json-转换
js转换
引用json.js(将json格式转换成字符串
var name = document.getElementById("name").value; var retries = document.getElementById("retries").value; var testname = document.getElementById("testname").value; var testretries = document.getElementById("testretries").value; //定义一个JSON对象 var paramObject= {"name":name,"retries":retries,"testname":testname,"testretries":testretries}; //将json对象转换成字符串传递到后台 var paramMap=JSON.stringify(paramObject);
java转换
java中直接遍历成Map:
/** * 将json格式的字符串解析成Map对象 <li> * json格式:{"name":"admin","retries":"3fff","testname" * :"ddd","testretries":"fffffffff"} */ private static HashMap<String, String> toHashMap(Object object) { HashMap<String, String> data = new HashMap<String, String>(); // 将json字符串转换成jsonObject JSONObject jsonObject = JSONObject.fromObject(object); Iterator it = jsonObject.keys(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()) { String key = String.valueOf(it.next()); String value = (String) jsonObject.get(key); data.put(key, value); } return data; }
或者转换成字符串对象:
获取国际化key和value
/** * 将json格式的字符串解析成String字符串平装<li> * json格式:String objString = "{\"aa\":1,\"cc\":2,\"bb\":3}"; */ @SuppressWarnings( {"unchecked"}) private String toAnnotationStr(Object object) { // HashMap<String, String> data = new HashMap<String, String>(); // 将json字符串转换成jsonObject if (object != null && !object.equals("")) { JSONObject jsonObject = JSONObject.fromObject(object); Iterator it = jsonObject.keys(); StringBuilder strBuilder = new StringBuilder(); // 遍历jsonObject数据,添加到Map对象 while (it.hasNext()) { String key = String.valueOf(it.next()); String value = (String) jsonObject.get(key); //TODO strBuilder.append(this.getText(key)) .append(this.getText("lego.lab.com.colon")) .append(value + " "); } return strBuilder.toString(); } else { return ""; } }
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法