JSON的几种拼接方式
去年这个时间开始参加的培训班,至此已经走过了一年的时间,发篇博客来纪念一下逝去的一年,代码是你最好的朋友,给你感动,给你悲伤,让你愤怒,让你兴奋,让你进步,五味陈杂,胜似生活。犹记得培训班老师每次踩了坑之后都会说,生活嘛—–透露着沉稳和自信
这两天一直在敲关于JSON的代码,JSON有三种拼接方式,xml,json串,JSONObject和JSONArray,我对xml了解的不多,只存在浅层次的了解,但是通过这次学习,也算是对xml有了一些了解。来自于w3school.com.cn
···xml是一种可扩展标记语言
···xml是一种标记语言,类似于HTML
···xml的设计宗旨是传输数据,而不是显示数据
···xml应用于web的许多方面,常用于简化数据存储和共享
废话不多说,直接上代码<?xml version="1.0" encoding="UTF-8"?>
<country>
<name>中国</name>
<province>
<name>河北</name>
<cities>
<city>石家庄</city>
<city>保定</city>
<city>衡水</city>
</cities>
</province>
<province>
<name>广东</name>
<cites>
<city>汕头</city>
<city>广州</city>
<city>佛山</city>
</cites>
</province>
<province>
<name>云南</name>
<cities>
<city>丽江</city>
<city>昆明</city>
<city>大理</city>
</cities>
</province>
<province>
<name>湖北</name>
<cities>
<city>武汉</city>
<city>襄阳</city>
<city>荆门</city>
</cities>
</province>
</country>
第二种方式是直接用json串进行拼接
{"name":"中国","province":[{"name":"河北","cities":{"city":["保定","石家庄","衡水"]
}
},{"name":"湖北","cities":{"city":["武汉","荆门"]
}
},{"name":"黑龙江","cities":{"city":["哈尔滨","攀枝花"]
}
},{"name":"广东","cities":{"city ":["汕头","东莞","珠海"]
}
}]
}
第三种方式是直接用java代码
首先eclipse里面是没有json的jar包的,需要从网上下载
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import Util.JsonUtil;publicclass JsonTest {publicstaticvoidmain(String[] args) {
JsonUtil ju =new JsonUtil();
String s = ju.createJson();if(s !=null){
JSONObject json = JSONObject.fromObject(s);
System.out.println("班级" + json.getString("班级"));
System.out.println("班级人数" + json.getString("班级人数"));
JSONArray array = json.getJSONArray("学生");for(int i =0;i < array.size();i++){
JSONObject jo = array.getJSONObject(i);
System.out.println("学生姓名" + jo.getString("姓名"));
System.out.println("学生年龄" + jo.getString("年龄"));
}
}
}
}
---------------------------------------------------------------------
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;publicclass JsonUtil {public StringcreateJson(){
JSONObject obj1 =new JSONObject();
obj1.put("姓名","张三");
obj1.put("年龄","18");
JSONObject obj2 =new JSONObject();
obj2.put("姓名","李四");
obj2.put("年龄","19");
JSONObject obj3 =new JSONObject();
obj3.put("姓名","王五");
obj3.put("年龄","20");
JSONArray array =new JSONArray();
array.add(obj1);
array.add(obj2);
array.add(obj3);
JSONObject obj =new JSONObject();
obj.put("班级","4134170801");
obj.put("班级人数","27");
obj.put("学生", array);
System.out.println(obj.toString());return obj.toString();
}
}
{"班级":"4134170801","班级人数":"27","学生":[{"姓名":"张三","年龄":"18"},{"姓名":"李四","年龄":"19"},{"姓名":"王五","年龄":"20"}]}
班级4134170801
班级人数27
学生姓名张三
学生年龄18
学生姓名李四
学生年龄19
学生姓名王五
学生年龄20
第二种方式
import net.sf.json.JSONObject;
import Util.Student;publicclass JsonTest1 {publicstaticvoidmain(String[] args) {
Student zhangsan =new Student("张三","18");
Student lisi =new Student("李四","19");
Student lixuuan =new Student("王五","20");
JSONArray array =new JSONArray();
array.add(zhangsan);
array.add(lisi);
array.add(lixuuan);
JSONObject obj =new JSONObject();
obj.put("学生",array);
System.out.println(obj.toString());
}
}
---------------------------------------------------------------------publicclass Student {private String name;private String age;publicStudent(String name,String age){this.name = name;this.age = age;
}public StringgetName() {return name;
}publicvoidsetName(String name) {this.name = name;
}public StringgetAge() {return age;
}publicvoidsetAge(String age) {this.age = age;
}
}
输出内容:
{"学生":[{"age":"18","name":"张三"},{"age":"19","name":"李四"},{"age":"20","name":"王五"}]}