第一个元素,json对象 ?? ?格式:{名称:数据值,名称:数据值,}1.什么是JSON?
JSON(JavaScript?Object Notation, JS 对象简谱) 采用完全独立于编程语言的用文本格式来存储和表示数据的轻量级的数据交换格式。
2.JSON的作用是什么?
存储和表示数据的文本格式。
3.如何编写JSON?
JSON是由2个元素组成
第一个元素JSON对象
格式:
{
? ?键 : 数据? ,
? ? 键: 数据值?
}
第二个元素JSON数组
格式:
[具体的数据值/json对象]
名称:数据值:键值对 ?? ?键值对中的键要有“”,键值对中的值字符串和时间日期型的数据需要“”。
例如:(类似于JavaScript数组)
[
??{"stuid":1001,"stuname":"zhangsan","stuage":23,"stuaddress":"西安"},
??{"stuid":1002,"stuname":"lisi","stuage":24,"stuaddress":"北京"},
??{"stuid":1003,"stuname":"wangwu","stuage":25,"stuaddress":"上海"}
]
4.生成JSON
第三方的开发包生成json数据
- json-simple-1.1.jar
- gson-2.8.0.jar
- jackson
[不推荐]
1)json-simple-1.1.jar生成JSON
/**
* simple生成json
* @param studentlists
*/
public static void createjson1(List<Student> studentlists){
//创建studentlist对应的json数组
JSONArray studentarr = new JSONArray();
for (Student stu : studentlists) {
//创建Student对象对应的json对象
JSONObject stujsonobj = new JSONObject ();
stujsonobj.put("stuid", stu.getStuid());
stujsonobj.put("stuname", stu.getStuname());
stujsonobj.put("stuage", stu.getStuage());
JSONArray addressarr = new JSONArray();
Address address[] = stu.getAddress();
for (Address item : address) {
JSONObject itemaddres = new JSONObject();
itemaddres.put("type", item.getType());
itemaddres.put("info", item.getInfo());
addressarr.add(itemaddres);
}
stujsonobj.put("address", addressarr);
studentarr.add(stujsonobj);
}
String jsonstr = studentarr.toString();
System.out.println(jsonstr);
}
2) gson-2.8.0.jar生成JSON
/***
* Gson生成json
* @param stulist
*/
public static void createjson2(List<Student> stulist){
Gson gson = new Gson();
String jsonstr = gson.toJson(stulist);
System.out.println(jsonstr);
}
3)jackson生成json
/**
* jackson生成json
* @param stulist
*/
public static void createjson3(List<Student> stulist){
ObjectMapper om = new ObjectMapper();
String json = "[]";
try {
json = om.writeValueAsString(stulist);
BufferedWriter outbff = new BufferedWriter(new FileWriter("stu.json"));
outbff.write(json);
outbff.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(json);
}
5.解析JSON
第三方的开发包生成json数据
- json-simple-1.1.jar
- gson-2.8.0.jar
- jackson.jar
1)json-simple-1.1.jar解析
/***
* 通过simple的解析json
* @return
*/
public static List<Student> readjsonfile1(){
List<Student> stuslist = new ArrayList<Student>();
//构造json构造器
JSONParser jsonParser = new JSONParser();
try {
JSONArray stusarr = (JSONArray) jsonParser.parse(new FileReader("stu.json"));
for (int i = 0; i < stusarr.size(); i++) {
Student stu = new Student();
JSONObject jsonobj = (JSONObject) stusarr.get(i);
long item = (Long) jsonobj.get("stuid");
int stuid = (int) item;
String stuname =jsonobj.get("stuname").toString();
item = (Long)jsonobj.get("stuage");
int stuage = (int )item;
JSONArray addarr = (JSONArray) jsonobj.get("address");
Address addressarr[] = new Address[addarr.size()];
for (int j = 0; j < addarr.size(); j++) {
JSONObject jsonObject = (JSONObject) addarr.get(j);
String type = jsonObject.get("type").toString();
String info = jsonObject.get("info").toString();
Address add = new Address(type, info);
addressarr[j] = add;
}
stu.setStuid(stuid);
stu.setStuname(stuname);
stu.setStuage(stuage);
stu.setAddress(addressarr);
stuslist.add(stu);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stuslist;
}
2)Gson解析JSON
/***
* 通过Gson解析json文件中的数据
* @param filename
* @return
* @throws JsonIOException
* @throws JsonSyntaxException
* @throws FileNotFoundException
*/
public static List<Student> readjsonfile2(String filename) throws JsonIOException,
JsonSyntaxException, FileNotFoundException{
Gson gson = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType();
return gson.fromJson(new FileReader(filename),type);
}
3)jackson
public static List<Student> readjsonfile3(String filename){
List<Student> stulist = new ArrayList<Student>();
ObjectMapper objmmp = new ObjectMapper ();
try {
JsonNode stuarr = objmmp.readTree(new File(filename));
for (int i = 0; i < stuarr.size(); i++) {
JsonNode stuinfo = stuarr.get(i);
int stuid = stuinfo.get("stuid").asInt();
String stuname = stuinfo.get("stuname").asText();
int stuage = stuinfo.get("stuage").asInt();
JsonNode addarr = stuinfo.get("address");
Address addressarr[] = new Address[addarr.size()];
for (int j = 0; j < addarr.size(); j++) {
JsonNode arrinfo = addarr.get(j);
String type = arrinfo.get("type").asText();
String info = arrinfo.get("info").asText();
Address add = new Address(type,info);
addressarr[j] = add;
}
Student stu = new Student(stuid,stuname,stuage,addressarr);
stulist.add(stu);
}
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stulist;
}
Gson虽然简单,但是需要提前构造号java中的数据结构,要和JSON中的数据一一对应
无奈源于不够强大
?
|