json编码;是一个轻量级的数据存储和传输的语言
在java中需要引入json相关的包,引包方式在工程的lib下就可以了
JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非
常适合于服务器与 JavaScript 之间的数据的交互)
json的表达式相当于JavaScript的原始类的声明方式相似
var obj={ name:"javascript", age:20 };
将java转成json的编码表达式;
1, 创建json的基本对象
String str="{}";//将java转化为json必须要先创建需要转换的数据 JSONObject jsonobj =new JSONObject.fromObject(str);//将java数据装换为json的编码方式
2,使用for循环几个数字放入创建的json对象中
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}
{"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的基本对象 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()); } // put 一个JSONObject对象 JSONObject obj1 = JSONObject.fromObject(date); for (int n = 0; n < 5; n++) { obj1.put("date"+n, n);//对象中放入5个数据 jsonobj.put("jsonobj", obj1);//将创建的obj1放入jsonobj对象中 System.out.println(jsonobj.toString()); }
结果:
{"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中
// 创建json的基本对象 String date = "{}"; JSONObject jsonobj = JSONObject.fromObject(date); //,将数组转化为JSONonject String[] str={"json","ajax","Jquery","javascript"};//创建要转化的数组的值 jsonobj.put("arr", str);//放到json对象中 System.out.println(jsonobj.toString());
结果:
{"arr":["json","ajax","Jquery","javascript"]}
5,将list放到json中
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中
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放到队列中
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是一个对象数组)
//思路: // 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的基本对象 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的基本对象 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"]}
]}