JSON:
也是一個标記語言,他的好處是解析比xml友善的多
JSON:
需要引入一個,json包
簡單的取值與指派
package com.kaige123.json;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
Student student=new Student();//建立對象
student.setName("李四");//給對象指派
student.setAddress("湖南");
student.setAge(12);
student.setEmail("[email protected]");
JSONObject jsonObject=new JSONObject(student);//将對象的值交給JSONObject對象
System.out.println(jsonObject.getString("name"));//可以通過指派的對象來取對象中的值
String jsonStr=jsonObject.toString();//将對象中的json值給 jsonStr
jsonObject=new JSONObject(jsonStr);//将值交給JSONObject對象
System.out.println(jsonObject.getString("name"));//通過對象中的方法來取值
}
}
JSON:json的解析原理
package com.kaige123.json;
import org.json.JSONObject;
public class TestJieXi {
public static void main(String[] args) {
String josnStr="{\"address\":\"湖南\",\"name\":\"李四\",\"age\":12,\"email\":\"[email protected]\"}";//将值交給字元串
JSONObject jsonObject=new JSONObject(josnStr);//将值交給JSONObject對象
System.out.println(jsonObject.getString("name"));//通過方法解析對象,拿到對象中的值
System.out.println(jsonObject.getString("address"));
System.out.println(jsonObject.getString("email"));
System.out.println(jsonObject.getInt("age"));
}
}
JSON:對象嵌套
public class Dog {
private String name;
private float age;
}
package com.kaige123.json;
public class Student {
private String name;
private String address;
private String email;
private int age;
private Dog dog;
}
嵌套對象取值:
package com.kaige123.json;
import org.json.JSONObject;
public class TestStudentDog {
public static void main(String[] args) {
Student student = new Student();
student.setName("張三");
student.setEmail("[email protected]");
student.setAge(12);
student.setAddress("湖南郴州");
Dog dog = new Dog();
dog.setName("旺财");
dog.setAge(12);
student.setDog(dog);
JSONObject jsonObject = new JSONObject(student);
System.out.println(jsonObject.toString());
//JSONObject student的值{"address":"湖南郴州","name":"張三","dog":{"name":"旺财","age":12},"email":"[email protected]","age":12}
System.out.println("name:" + jsonObject.getString("name"));
JSONObject jsonObject1 = jsonObject.getJSONObject("dog");
//那到jsonObject對象 裡面是student對象 再通過方法拿到dog對象交給jsonObject1
System.out.println(jsonObject1.getString("name"));
//再通過jsonObject1拿到dog中的屬性
}
}
JSON:數組對象取值
package com.kaige123.json;
import org.json.JSONArray;
import org.json.JSONObject;
public class TestStudentDogArray {
public static void main(String[] args) {
Student[] students=new Student[2];//建立對象數組
Student student=new Student();
student.setName("張三");
student.setEmail("[email protected]");
student.setAge(12);
student.setAddress("湖南郴州");
Dog dog=new Dog();
dog.setName("旺财");
dog.setAge(12);
student.setDog(dog);
students[0]=student;//給數組對象指派
student=new Student();
student.setName("張三1");
student.setEmail("[email protected]");
student.setAge(14);
student.setAddress("湖南郴州1");
dog=new Dog();
dog.setName("旺财1");
dog.setAge(121);
student.setDog(dog);
students[1]=student;//給數組對象指派
JSONArray jsonObject=new JSONArray(students);
//對象的接口 JSONArray 将數組對象交給JSONArray接口
System.out.println(jsonObject);
String jsonStr=jsonObject.toString();
JSONArray jsonArray=new JSONArray(jsonStr);
//[{"address":"湖南郴州","name":"張三","dog":{"name":"旺财","age":12},"age":12,"email":"[email protected]"},
// {"address":"湖南郴州1","name":"張三1","dog":{"name":"旺财1","age":121},"age":14,"email":"[email protected]"}] 兩個對象值
for(int i=0;i<jsonArray.length();i++){//通過jsonArray.length()方法拿到數組長度
JSONObject jsonObject1=jsonArray.getJSONObject(i);
//json取值接口JSONObject 通過數組對象中的jsonArray.getJSONObject(i)方法來獲得數組中的一個個對象
System.out.println(jsonObject1.getString("name"));
System.out.println(jsonObject1.getString("address"));
System.out.println(jsonObject1.getString("email"));
System.out.println(jsonObject1.getInt("age"));
JSONObject jsonObject2=jsonObject1.getJSONObject("dog");
//嵌套對象取值通過jsonObject1嵌套對象中的jsonObject1.getJSONObject("dog")方法來取得被嵌套對象的資料
System.out.println("----Dog-----");
System.out.println(jsonObject2.getString("name"));//再通過方法取值
System.out.println(jsonObject2.getFloat("age"));
System.out.println("----Dog-----");
}
}
}
JSON:集合對象取值
package com.kaige123.json;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Vector;
public class TestStudentDogVector {
public static void main(String[] args) {
Vector vector = new Vector();
//建立集合對象
//集合比數組要厲害 因為可以添加很多個對象 數組是控制長度的儲存器
Student student = new Student();
student.setName("張三");
student.setEmail("[email protected]");
student.setAge(12);
student.setAddress("湖南郴州");
Dog dog = new Dog();
dog.setName("旺财");
dog.setAge(12);
student.setDog(dog);
vector.add(student);//将對象交給集合
student = new Student();
student.setName("張三1");
student.setEmail("[email protected]");
student.setAge(14);
student.setAddress("湖南郴州1");
dog = new Dog();
dog.setName("旺财1");
dog.setAge(121);
student.setDog(dog);
vector.add(student);//将對象交給集合
JSONArray jsonObject = new JSONArray(vector);//建立接受集合對象
for (int i = 0; i < jsonObject.length(); i++) {
JSONObject jsonObject1 = jsonObject.getJSONObject(i);//将集合中的對象給 JSONObject對象
System.out.println(jsonObject1.getString("name"));//通過JSONObject對象來拿去對象中的值
System.out.println(jsonObject1.getString("address"));
System.out.println(jsonObject1.getString("email"));
System.out.println(jsonObject1.getInt("age"));
JSONObject jsonObject2 = jsonObject1.getJSONObject("dog");
//嵌套對象取值通過jsonObject1嵌套對象中的jsonObject1.getJSONObject("dog")方法來取得被嵌套對象的資料
//将值叫給JSONObject對象再通過對象來拿去對象中的值
System.out.println("----Dog-----");
System.out.println(jsonObject2.getString("name"));
System.out.println(jsonObject2.getFloat("age"));
System.out.println("----Dog-----");
}
}
}
JSON:中的忽略與添加
package com.kaige123.json;
import org.json.JSONObject;
import org.json.JSONTokener;
public class TestStudentDogQuChu {
public static void main(String[] args) {
Student student=new Student();
student.setName("張三");
student.setEmail("[email protected]");
student.setAge(12);
student.setAddress("湖南郴州");
Dog dog=new Dog();
dog.setName("旺财");
dog.setAge(12);
student.setDog(dog);
JSONObject jsonObject=new JSONObject(student);
jsonObject.remove("email");//取出對象中的某一行資料、每個JSON插件中的忽略代碼都不一樣
jsonObject.remove("address");
jsonObject.put("phone","13900000000");//添加一行資料 隻是這個對象
System.out.println(jsonObject.toString());
}
}
JSON中的API查詢
public class URLConn {
//連接配接代碼
public static String openAPI(String url, String charset) {
String body = "";
try {
URLConnection conn = new URL(url).openConnection();
conn.connect();
conn.setReadTimeout(50000);//讀取逾時
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
conn.getInputStream(), charset));
StringBuffer stringBuffer = new StringBuffer();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
bufferedReader.close();
return stringBuffer.toString();
} catch (Exception e) {
System.out.println("通路出錯");
e.printStackTrace();
}
return body;
}
}
package com.kaige123.json;
import org.json.JSONObject;
public class TestAPIJson {
public static void main(String[] args) {
String body=URLConn.openAPI(
"http://v.juhe.cn/sms/send?mobile=&tpl_id=52299&tpl_value=%23code%23%3D&key=",//請求執行個體
"UTF-8");//編碼格式
System.out.println(body);//列印結果
}
}