JSON:
也是一个标记语言,他的好处是解析比xml方便的多
JSON:
需要引入一个,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("123@qq.com");
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\":\"123@qq.com\"}";//将值交给字符串
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("123@qq.com");
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":"123@qq.com","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("123@qq.com");
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("123@qq.com1");
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":"123@qq.com"},
// {"address":"湖南郴州1","name":"张三1","dog":{"name":"旺财1","age":121},"age":14,"email":"123@qq.com1"}] 两个对象值
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("123@qq.com");
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("123@qq.com1");
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("123@qq.com");
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);//打印结果
}
}