一、Json数据解析:对象类型、数组类型({“属性”:“键值”}、{“key”:“valus”})
对象类型:{“属性1”:“键值1”,“属性2”:“键值2”}
数组类型:[{“属性1”:“键值1”,“属性1”:“键值2”}]
模拟数据:[{“id”:”5”,”version”:”5.5”,”name”:”Tom”},{“id”:”6”,”version”:”6.6”,”name”:”Lucky”}]
(1)、使用JSONObject解析
数组解析:
public void parseJSONWithJSONObject(String jsonData){
try{
JSONArray jsonArray = new JSONArray(jsonData);
for(int i = 0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.i("TAG","id="+id);
Log.i("TAG","name="+name);
Log.i("TAG","version="+version);
}
}catch (Exception e){
e.printStackTrace();}
}
普通解析:
public void parseJSONWithJSONObject(String jsonData){
try{
JSONObject jsonObject = new JSONObject(jsonData);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.i("TAG","id="+id);
Log.i("TAG","name="+name);
Log.i("TAG","version="+version);
}catch (Exception e){
e.printStackTrace();}
}
(2)、使用GSON解析:
public void parseJSONWithGSON(String jsonData){
Gson gson = new Gson();
//<>里面放关联的对象
List<App> appList = gson.fromJson(jsonData, new TypeToken< List<App>>(){}.getType());
//写出对象,在用实例获取。
for(App app:appList){
Log.i("TAG","id="+app.getId());
Log.i("TAG","name="+app.getName());
Log.i("TAG","version="+app.getVersion());
}
}
(3)、使用fastJson解析
创建实体
public class Student {
private int id;
private String name;
private int age;
/**
* 默认的构造方法必须不能省,不然不能解析
*/
public Student(){
}
public Student(int id,String name,int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
//对象转化成json数组(序列化)
Student student = new Student(0,"along",24);
System.out.println(JSON.toJSONString(student));
//json数组转化成对象(反序列化)
String data = "{\"id\":\"0\",\"name\":\"along\",\"age\":\"24\"}";
Student student = JSON.parseObject(data,Student.class);
Log.i("TAG","student="+student);
//日期格式化
Student student=new Student();
//输出毫秒值
System.out.println(JSON.toJSONString(student));
//默认格式为yyyy-MM-dd HH:mm:ss
System.out.println(JSON.toJSONString(student, SerializerFeature.WriteDateUseDateFormat));
//根据自定义格式输出日期
System.out.println(JSON.toJSONStringWithDateFormat(student, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat));
二、Xml数据解析
(1)、Pull解析
<apps>
<app>
<id>1</id>
<name>Google Maps</name>
<version>1.1</version>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.2</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>3.3</version>
</app>
</apps>
public void parseXMLWhitPull(String xmlData){
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlData));
int eventType = xmlPullParser.getEventType();
String id = "";
String name = "";
String version = "";
while (eventType !=XmlPullParser.END_DOCUMENT){
String nodeName = xmlPullParser.getName();
switch (eventType){
case XmlPullParser.START_TAG:{
if("id".equals(nodeName)){
id = xmlPullParser.nextText();
}else if("name".equals(nodeName)){
name = xmlPullParser.nextText();
}else if("version".equals(nodeName)){
version = xmlPullParser.nextText();
}
break;
}
case XmlPullParser.END_TAG:{
if("app".equals(nodeName)){
Log.i("TAG","id="+id);
Log.i("TAG","name="+name);
Log.i("TAG","version="+version);
}
break;
}
default:
break;
}
eventType = xmlPullParser.next();
}
}catch (Exception e){
e.printStackTrace();
}
}