java将数据转成json编码格式技术实例教程
json编码;是一个轻量级的数据存储和传输的语言
在java中需要引入json相关的包,引包方式在工程的lib下就可以了
JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非
常适合于服务器与 JavaScript 之间的数据的交互)
json的表达式相当于JavaScript的原始类的声明方式相似
<table width="620" cellspacing="1" cellpadding="1" border="0" align="center" style="background:#FB7"> <tbody> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" bgcolor="#FFE7CE" align="center" style="cursor:pointer;" onclick="doCopy('copy3327')">复制代码</td> </tr> <tr> <td valign="top" height="auto" bgcolor="#FFFFFF" colspan="2" style="padding:10px;" class="copyclass" id="copy3327">var obj={
name:"javascript",
age:20
}; </td> </tr> </tbody> </table>
将java转成json的编码表达式;
1, 创建json的基本对象
Json代码
String str="{}";//将java转化为json必须要先创建需要转换的数据
JSONObject jsonobj =new JSONObject.fromObject(str);//将java数据装换为json的编码方式
2,使用for循环几个数字放入创建的json对象中
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2007')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2007>for(int i=0;i<5;i ){
jsonobj.put("json" i,i);//将java数据放到json对象中去
// jsonobj.put("json" i, i);和put的执行结果一样
// jsonobj.accumulate("json" i, i);和put的执行结果一样
System.out.println(jsonobj.toString());//打印json的数据
}
Json代码
结果:
{"id0":0}
{"id0":0,"id1":1}
{"id0":0,"id1":1,"id2":2}
{"id0":0,"id1":1,"id2":2,"id3":3}
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4}
3,使用put向json中放一个JSONObject对象
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3034')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3034>for(int i=0;i<5;i ){
jsonobj.put("json" i,i);//将java数据放到json对象中去
// jsonobj.put("json" i, i);和put的执行结果一样
// jsonobj.accumulate("json" i, i);和put的执行结果一样
System.out.println(jsonobj.toString());//打印json的数据
}
结果:
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"jsonobj":{"date0":0}}
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"jsonobj":{"date0":0,"date1":1}}
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"jsonobj":{"date0":0,"date1":1,"date2":2}}
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"jsonobj":{"date0":0,"date1":1,"date2":2,"date3":3}}
{"id0":0,"id1":1,"id2":2,"id3":3,"id4":4,"jsonobj":{"date0":0,"date1":1,"date2":2,"date3":3,"date4":4}}
4,将数组添加到json中
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9007')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9007> // 创建json的基本对象
String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);
// 使用int将数值放到json
for (int j = 0; j < 5; j ) {
// jsonobj.put("id" j, j);
// jsonobj.accumulate("id" j, j);
jsonobj.element("id" j, j);
//System.out.println(jsonobj.toString());
}
结果:
{"arr":["json","ajax","jquery","javascript"]}
5,将list放到json中
Java代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2076')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2076>
String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);
ArrayList<String> list = new ArrayList<String>();
list.add("json");
list.add("java");
list.add("android");
jsonobj.put("jsonobj", list);
System.out.println(jsonobj.toString());
结果:{"jsonobj":["json","java","android"]}
6,将map放到json中
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9456')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9456>String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);//创建转化的对象
Map<String,Object> map = new HashMap<String, Object>();//创建map队列
map.put("map1", "json");
map.put("map2", "java");
map.put("map3", "android");
jsonobj.put("map", map); //放到json中
System.out.println(jsonobj.toString());
结果:
{"map":{"map3":"android","map2":"java","map1":"json"}}
7,使用 putAll 将json放到队列中
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1173')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1173> String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);//创建转化的对象
Map<String,Object> map = new HashMap<String, Object>();
map.put("map1", "json");
map.put("map2", "android");
map.put("map3", "Jquery");
jsonobj.putAll(map);
System.out.println(jsonobj.toString());
结果:
{"map3":"Jquery","map2":"android","map1":"json"}
8,区分put和putAll的区别:
put;会有一个对象 {"map":{"map3":"android","map2":"java","map1":"json"}}
putAll没有 {"map3":"Jquery","map2":"android","map1":"json"}
9, 组合: (返回的结果value是一个对象数组)
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4055')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4055>//思路:
// 1,创建转化为json的对象
// 2,创建ArrayList队列存放转化的json对象
// 3,使用for循环3次,创建json的对象,向循环创建的json添加值
// 4,将循环创建的json对象放到队列中
// 5,将队列添加到json中
String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);//创建转化的对象
ArrayList<JSONObject> list = new ArrayList<JSONObject>();//创建队列
for(int i=0; i<3; i ){ //循环
JSONObject json = JSONObject.fromObject(date); //转化的java
json.put("aaaa" i, i);
json.put("bbbb" i, i);
json.put("cccc" i, i);
list.add(json);
}
jsonobj.put("succList", list);
System.out.println(jsonobj.toString());
结果:
{"succList":[
{"aaaa0":0,"bbbb0":0,"cccc0":0},
{"aaaa1":1,"bbbb1":1,"cccc1":1},
{"aaaa2":2,"bbbb2":2,"cccc2":2}
] }
10,json的组合,返回的结果的值是一个数组
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1727')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1727>// 创建json的基本对象
String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);
String[] str={"json","ajax","Jquery","javascript"};//数组
jsonobj.put("arr", str);
ArrayList<JSONObject> list = new ArrayList<JSONObject>();//创建队列
for(int i=0; i<3; i ){ //循环
JSONObject json = JSONObject.fromObject(date); //转化的java
json.put("aaaa" i, i);
json.put("bbbb" i, i);
json.put("cccc" i,str ); //添加一个数组
list.add(json);
}
jsonobj.put("succList", list);
System.out.println(jsonobj.toString());
结果:
{"arr":["json","ajax","Jquery","javascript"],
"succList":[{"aaaa0":0,"bbbb0":0,"cccc0":["json","ajax","Jquery","javascript"]},
{"aaaa1":1,"bbbb1":1,"cccc1":["json","ajax","Jquery","javascript"]},
{"aaaa2":2,"bbbb2":2,"cccc2":["json","ajax","Jquery","javascript"]}
]}
11,组合,任意的嵌套;
Json代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7423')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7423>// 创建json的基本对象
String date = "{}";
JSONObject jsonobj = JSONObject.fromObject(date);
String[] str={"json","ajax","Jquery","javascript"};//数组
//转化java的json嵌套到json中
JSONObject jsonDemo = JSONObject.fromObject(date);
for(int n=0;n<3;n ){
jsonDemo.put("组合" n, n);
}
ArrayList<JSONObject> list = new ArrayList<JSONObject>();//创建队列
for(int i=0; i<3; i ){ //循环
JSONObject json = JSONObject.fromObject(date); //转化的java
json.put("aaaa" i, jsonDemo); //嵌套的json
json.put("bbbb" i, "java");
json.put("cccc" i,str ); //值为一个数组
list.add(json);
}
jsonobj.put("succList", list);
System.out.println(jsonobj.toString());
运行结果:
{"succList":
[{"aaaa0":{"组合0":0,"组合1":1,"组合2":2},
"bbbb0":"java","cccc0":["json","ajax","Jquery","javascript"]},
{"aaaa1":{"组合0":0,"组合1":1,"组合2":2},
"bbbb1":"java","cccc1":["json","ajax","Jquery","javascript"]},
{"aaaa2":{"组合0":0,"组合1":1,"组合2":2},
"bbbb2":"java","cccc2":["json","ajax","Jquery","javascript"]}
]}
您可能感兴趣的文章:
java将数据转成json编码格式技术实例教程
Python JSON
jquery转化Datatable为json数据的例子
php解析JSON中文乱码问题的解决方法
php中操作json格式数据转换实现代码
Go Web编程--解析JSON请求和生成JSON响应
PHP JSON
php怎么传给java
JSP常见问题
Java 实例