数据传输类型
姓名:张三、年龄:20、地址:[城市:北京、街道:长安街]
- 1. xml数据类型
<person>
<name>张三</name>
<age>20</age>
<adress>
<city>北京</city>
<street>长安街</street>
</adress>
</person>
- 2. json数据类型(重要)
相比xml数据类型,json数据需要的字节数更少,在数据传输过程中,即需要更少的流量,所以json数据的使用更广泛。
{
"name": "张三",
"age": 20,
"address":{
"city": "北京",
"street": "长安街"
}
}
- 3. 解析json数据
public static void main(String[] args) {
File file = new File("C:/Users/Administrator/Desktop/课堂图片/Person.json");
String strJson = getText(file);// 读取本地json数据
// json解析,并生成对象
Person p = parseJson(strJson);
System.out.println(p);
}
/**
* 解析String里的json数据并返回一个Person对象
*/
public static Person parseJson(String json){
Person person = null;
try {
JSONObject jsonObject = new JSONObject(json);
// 通过key取value
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
// 获取address对应的json对象
JSONObject jsonAdress = jsonObject.getJSONObject("address");
// 通过jsonAddress取得city和street
String city = jsonAddress.getString("city");
String street = jsonAddress.getString("street");
Address address = new Address(city,street);
// 赋值给实体类的属性
person = new Person();
person.setName(name);
person.setAge(age);
person.setAddress(address);
} catch (JSONException e) {
e.printStackTrace();
}
return person;
}
/**
* 将本地json文件内的数据解析为字符串
*/
public static String getText(File file){
BufferedReader br = null;
StringBuilder builder = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String str = null;
builder = new StringBuilder();
while((str=br.readLine())!=null){
builder.append(str);
builder.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return builder.toString();
}
- 4. 解析复杂的json数据
[{
"name": "张三",
"age": 20,
"address": [
{
"city": "北京",
"street": "长安街"
},
{
"city": "武汉",
"street": "长安街"
},
{
"city": "成都",
"street": "成龙大道"
}
]
},{
"name": "李四",
"age": 22,
"address": [
{
"city": "北京",
"street": "长安街"
},
{
"city": "武汉",
"street": "长安街"
},
{
"city": "成都",
"street": "成龙大道"
}
]
},{
"name": "王五",
"age": 23,
"address": [
{
"city": "北京",
"street": "长安街"
},
{
"city": "武汉",
"street": "长安街"
},
{
"city": "成都",
"street": "成龙大道"
}
]
}]
public static void main(String[] args) {
File file = new File("C:/Users/Administrator/Desktop/课堂图片/Person.json");
String strJson = IOUtils.getText(file);
// json解析,并生成对象
List<Person> list = parseJsonArray(strJson);
for (Person person : list) {
System.out.println(person);
}
}
public static List<Person> parseJsonArray(String json){
List<Person> list = null;
try {
JSONArray jsonArray = new JSONArray(json);
list = new ArrayList<Person>();
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
Person p = parseJson(jsonObject.toString());
list.add(p);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
public static Person parseJson(String json){
Person person = null;
try {
JSONObject jsonObject = new JSONObject(json);
// 通过key取value
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
// 获取address得到3个地址json数据组成的数组
JSONArray jsonArray = jsonObject.getJSONArray("address");
// 用于存放3个地址
List<Address> list = new ArrayList<Address>();
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonAddress = (JSONObject) jsonArray.get(i);
String city = jsonAddress.getString("city");
String street = jsonAddress.getString("street");
Address address = new Address(city,street);
list.add(address);
}
// 赋值给实体类的属性
person = new Person();
person.setName(name);
person.setAge(age);
person.setAddress(list);
} catch (JSONException e) {
e.printStackTrace();
}
return person;
}
- 5. 官方以外的jar包称为外包,用外包简化我们的解析代码
Google提供的外包:Gson
import com.google.gson.Gson;
public static void main(String[] args) {
File file = new File("C:/Users/Administrator/Desktop/课堂图片/Person.json");
String strJson = IOUtils.getText(file);
System.out.println(strJson);
Gson gson = new Gson();
// 通过gson,完成json数据的解析
Person p = gson.fromJson(strJson, Person.class);
System.out.println(p);
// 完成对象转json的功能
String str = gson.toJson(p);
System.out.println(str);
}
网络接口编程
接口链接网站:http://apistore.baidu.com/
手机号码归属地
其中ObjectMapper mapper = new ObjectMapper();使用了另一种第三方jar包(jackson)。