Json
Json是一种与开发语言无关的,轻量级的数据交换格式。全称(JavaScript Object Notation)。
简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
优点:易于人阅读和编写,易于程序解析。
数据类型表示
数据结构:Object, Array
基本类型:String , number , true , false ,null
json中不区分整数、小数等类型,而统一使用Number来存储数字。
1.数据结构 - Object
使用花括号{}包含键值队,Key必须是String类型的,value可以是任何数据结构和基本类型。key和value用分号分隔开。
2.数据结构 - Array
使用中括号[ ]来起始,并用逗号好分割元素。
Json数据演示
{
"name" : "Freddie",
"age" : 24,
"birthday" : "1996-01-01",
"school" : "HUST" ,
"major" : ["computer","software"],
"has_girlfriend" : false,
"car" : null,
"house" : null,
}
Json的使用
Maven中引入Json
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
1.通过JSON对象构建JSON
例子
用put方法增加student的属性,然后用tostring()打印出来
import org.json.JSONException;
import org.json.JSONObject;
public class Json {
public static void main( String[] args ){
JSONObject student = new JSONObject();
Object nullObj = null;
try {
student.put("name","Freddie");
student.put("age", 24);
student.put("birthday", "1996-01-01");
student.put("school", "HUST");
student.put("major", new String[]{"computer","software"});
student.put("has_girlfriend", false);
student.put("car", nullObj);
student.put("house",nullObj);
System.out.println(student.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.通过Map构建JSON
例子
public class Json {
public static void main( String[] args ){
Map<String,Object> student = new HashMap<String,Object>();
Object nullObj = null;
student.put("name","Freddie");
student.put("age", 24);
student.put("birthday", "1996-01-01");
student.put("school", "HUST");
student.put("major", new String[]{"computer","software"});
student.put("has_girlfriend", false);
student.put("car", nullObj);
student.put("house",nullObj);
System.out.println(new JSONObject(student).toString());
}
}
3.使用Java bean构建JSON
这是在项目中最多见的一种
例子
public class student {
private String name;
private String school;
private boolean has_girlfrend;
private double age;
private Object car;
private Object house;
private String[] major;
private String birthday;
.....setter getter方法
}
public class Json {
public static void main( String[] args ){
Student a = new student();
a.setName("Freddie");
a.setAge(24);
a.setSchool("HUST");
a.setBirthday("1996-01-01");
a.setMajor(new String[]{"computer","software"});
a.setHas_girlfriend(false);
a.setCar(null);
a.setHouse(null);
System.out.println(new JSONObject(a));
}
}
4.从文件中读取JSON
假设我们要读取同一路径下的 student.json文件
public class ReadJSON {
public static void main( String[] args ){
File file = new File(ReadJSON.class.getResource("/student.json"));
//我们需要的内容已经在content中了
String content = FileUtils.readFileToString(file);
JsonObject jsonObject = new JsonObject(content);
System.out.println("姓名是:"+ jsonObject.getString("name"));
System.out.println("年龄是:"+ jsonObject.getdouble("age"));
JSONArray majorArray = jsonObject.getJsonArray("major");
for(int i =0; i<majorArray.length(); i++){
String m = (String)majorArray.get(i);
System.out.println("专业是:"+(i+1)+m);
}
}
}