json 转换; jsp页面,java后台,json转换总结
一:jsp json的转换
jquery js包下载地址:http://www.jq22.com/jquery-info122
<html>
<head>
<title>json测试</title>
<meta charset="utf-8">
<script src="jquery-3.2.1.js"></script>
<script src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function json(){
var value="{'aaa':'111'}";
var obj = eval('(' + value +' )');
alert(obj.aaa);
}
function jsonarray(){
var value="{'aaa':[{'a':'111'},{'a':'222'}] }";
var obj = eval('(' + value +' )');
var data=obj.aaa;
for(var o in data){
alert("text:"+data[o].a );
}
}
function jsonone(){
var value="{'aaa':{'bbb':'111'} }";
var obj = eval('(' + value +' )');
alert(obj.aaa.bbb);
}
</script>
</head>
<body>
<div>
<input type="button" onclick="json()" value="json" >
<input type="button" onclick="jsonarray()" value="json_array" >
<input type="button" onclick="jsonone()" value="jsonone" >
</div>
</body>
</html>
二:java后台json转换
public static void main(String args[])
{
JSONObject jsonObj = new JSONObject();
jsonObj.put("name0", "zhangsan");
jsonObj.put("sex1", "female");
System.out.println("JSONObject"+jsonObj); //输出为:{"sex1":"female","name0":"zhangsan"}
JSONArray jsonarray=new JSONArray();
jsonarray.put("aa");
jsonarray.put("bb");
jsonarray.put("cc");
System.out.println("JSONArray:"+jsonarray);
//可以由数组,列表等生成JSONArray
String[] list={"11","22"};
JSONArray jsonarray2 = JSONArray.fromObject(list);
System.out.println("可以由数组,列表等生成JSONArray:"+jsonarray2); //输出为:["11","22","33"]
//可以由Map生成JSONObject
Map<String,Object> map=new HashMap<String,Object>();
map.put("NO1", "第一个");
map.put("NO2", "第二个");
map.put("NO3", jsonarray2);
JSONObject jsonObj2 = JSONObject.fromObject(map);
System.out.println("可以由Map生成JSONObject"+jsonObj2); //输出为:{"NO3":["11","22","33"],"NO2":"第二个","NO1":"第一个"}
JSONArray jsonarray3;
JSONObject jsonObj3;
//读取JSONArray,用下3标索引获取
String array="[\"11\",\"22\",\"33\"]";
jsonarray3 = JSONArray.fromObject(array);
System.out.println(jsonarray3.getString(1)); //输出为:22
//读取JSONObject
String object="{\"NO1\":[\"44\",\"55\",\"66\"],\"NO2\":{\"NO1\":\"第一个\"}}";
jsonObj3 = JSONObject.fromObject(object);
System.out.println(jsonObj3.get("NO1")); //输出为:["44","55","66"]]
System.out.println(jsonObj3.get("NO2")); //输出为:["44","55","66"]
jsonarray3 = (JSONArray)(jsonObj3.get("NO1"));
System.out.println(jsonarray3.getString(1)); //输出为:55
//用"键"获取值
jsonObj3=(JSONObject)jsonObj3.get("NO2");
System.out.println(jsonObj3); //输出为:{"NO1":"第一个"}
//使用 JSONObject.toBean 方法,拥有两个实体类ResultAuthority,Access
String str22="{\"access\":{\"code\":\"0000\",\"msg\":\"成功\"},"
+ "\"result\":[{\"login_token\":\"914fa06f3ab74fa89137b42a54f95b8d\","
+ "\"login_key\":\"WwgfbPNr\","
+ "\"V002\":\"20170301142319000279\"}]}";
//String str2="{ \"access\":{\"msg\":\"12\"} ,\"result\":[{\"login_token\":\"lisan\"}]}";
JSONObject js2222=JSONObject.fromObject(str22);
Map classMap = new HashMap();
classMap.put("access",Access.class);
classMap.put("result",Map.class);
ResultAuthority aaa=(ResultAuthority) JSONObject.toBean(js2222, ResultAuthority.class, classMap);
System.out.println(aaa.getAccess().getCode());
}
----------------------------------------------------------
实体类:ResultAuthority
public class ResultAuthority implements java.io.Serializable{
private Access access;
private List result;
public List getResult() {
return result;
}
public void setResult(List result) {
this.result = result;
}
public Access getAccess() {
return access;
}
public void setAccess(Access access) {
this.access = access;
}
}
------------------------------------------------------------------
实体类Access
public class Access {
private String code;
private String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
--------------------------------------------------------