jsp
1.引用json.js(将json格式转换成字符串)
2.
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);
3.
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 "";
}
}