Json数据解析
https://code.google.com/p/google-gson/ 谷歌官方的jjson解析类库Gson
http://www.jsonschema2pojo.org/ 该网站能将Json数据解析为POJO(简单的java对象)。
1、单个对象
假设json数据如下:
{ "id": 100, "body": "It is my post", "number": 0.13, "created_at": "2014-05-22 19:12:38" }
通过网站自动解析得到的POJO如下(created_at被序列化为符合java规范的驼峰结构):
package com.example; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Example { @Expose private Integer id; @Expose private String body; @Expose private Float number; @SerializedName("created_at") @Expose private String createdAt; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Float getNumber() { return number; } public void setNumber(Float number) { this.number = number; } public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } }
最后就可以序列化了:
Example foo = new Gson().fromJson(JSON_DATA, Example.class); //Example为POJO类
2、对象的嵌套
假设json数据如下:
{ "id": 100, "body": "It is my post", "number": 0.13, "created_at": "2014-05-22 19:12:38", "foo2": { "ids": 200, "name": "haha" } }
通过网站自动解析得到的POJO如下:
-----------------------------------com.example.Example.java----------------------------------- package com.example; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Example { @Expose private Integer id; @Expose private String body; @Expose private Double number; @SerializedName("created_at") @Expose private String createdAt; @Expose private Foo2 foo2; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Double getNumber() { return number; } public void setNumber(Double number) { this.number = number; } public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } public Foo2 getFoo2() { return foo2; } public void setFoo2(Foo2 foo2) { this.foo2 = foo2; } } -----------------------------------com.example.Foo2.java----------------------------------- package com.example; import javax.annotation.Generated; import com.google.gson.annotations.Expose; @Generated("org.jsonschema2pojo") public class Foo2 { @Expose private Integer ids; @Expose private String name; public Integer getIds() { return ids; } public void setIds(Integer ids) { this.ids = ids; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
最后就可以序列化了:
Example foo = new Gson().fromJson(JSON_DATA, Example.class); Foo为POJO类
3、对象数组
假设json数据如下:
[{ "id": 100, "body": "It is my post1", "number": 0.13, "created_at": "2014-05-20 19:12:38" }, { "id": 101, "body": "It is my post2", "number": 0.14, "created_at": "2014-05-22 19:12:38" }]
使用网站自动解析得到POJO:
package com.example; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; @Generated("org.jsonschema2pojo") public class Example { @Expose private Integer id; @Expose private String body; @Expose private Float number; @SerializedName("created_at") @Expose private String createdAt; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Float getNumber() { return number; } public void setNumber(Float number) { this.number = number; } public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } }
这种解析有两种方法:
1、解析成数组:
Example[] foos = new Gson().fromJson(JSON_DATA, Example[].class);
2、解析成list:
Type listType = new TypeToken<ArrayList<Example>>(){}.getType(); ArrayList<Example> foos = new Gson().fromJson(JSON_DATA, listType);
XML数据解析
http://www.xmlpull.org/ pull解析器下载
http://kxml.sourceforge.net/ kxml包
假设我们的XML数据如下:
<root> <student id="1" group="1"> <name>张三</name> <sex>男</sex> <age>18</age> <email>zhangsan@163.com</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> <student id="2" group="2"> <name>李四</name> <sex>女</sex> <age>18</age> <email>lisi@163.com</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> <student id="3" group="3"> <name>小王</name> <sex>男</sex> <age>18</age> <email>xiaowang@163.com</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> <student id="4" group="4"> <name>小张</name> <sex>男</sex> <age>18</age> <email>xiaozhang@163.com</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> <student id="5" group="5"> <name>小明</name> <sex>男</sex> <age>18</age> <email>xiaoming@163.com</email> <birthday>1987-06-08</birthday> <memo>好学生</memo> </student> </root>
Xml对应的实体bean文件:
public class Student { private int id; private int group; private String name; private String sex; private int age; private String email; private String memo; private String birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getGroup() { return group; } public void setGroup(int group) { this.group = group; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMemo() { return memo; } public void setMemo(String memo) { this.memo = memo; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
Pull解析代码:
package com.parsexml; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import com.parsexml.entity.Student; public class PullParseXml { public List<Student> PullParseXML(){ List<Student> list=null; Student student = null; //构建XmlPullParserFactory try { XmlPullParserFactory pullParserFactory=XmlPullParserFactory.newInstance(); //获取XmlPullParser的实例 XmlPullParser xmlPullParser=pullParserFactory.newPullParser(); //设置输入流 xml文件 xmlPullParser.setInput(inputStream,"UTF-8"); //inputStream为网络上的到的XML格式的输入流 //开始 int eventType=xmlPullParser.getEventType(); try { while(eventType!=XmlPullParser.END_DOCUMENT){ String nodeName=xmlPullParser.getName(); switch (eventType) { //文档开始 case XmlPullParser.START_DOCUMENT: list=new ArrayList<Student>(); break; //开始节点 case XmlPullParser.START_TAG: //判断如果其实节点为student if("student".equals(nodeName)){ //实例化student对象 student=new Student(); //设置Id属性 student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0))); //设置Group属性 student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1))); }else if("name".equals(nodeName)){ //设置name student.setName(xmlPullParser.nextText()); }else if("sex".equals(nodeName)){ //设置sex student.setSex(xmlPullParser.nextText()); }else if("age".equals(nodeName)){ //设置age student.setAge(Integer.parseInt(xmlPullParser.nextText())); }else if("email".equals(nodeName)){ //设置email student.setEmail(xmlPullParser.nextText()); }else if("birthday".equals(nodeName)){ //设置birthday student.setBirthday(xmlPullParser.nextText()); }else if("memo".equals(nodeName)){ //设置memo属性 student.setMemo(xmlPullParser.nextText()); } break; //结束节点 case XmlPullParser.END_TAG: if("student".equals(nodeName)){ list.add(student); student=null; } break; default: break; } eventType=xmlPullParser.next(); } } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (XmlPullParserException e) { e.printStackTrace(); } return list; } public static void main(String[] args) { List<Student> list=new PullParseXml().PullParseXML(); for(Student student:list){ System.out.println("id:"+student.getId()+" group:"+student.getGroup()+" name:"+student.getName()+" sex:"+student.getSex()+" age:"+student.getAge()+" email:"+student.getEmail()+" birthday:"+student.getBirthday()+" memo:"+student.getMemo()); } } }
HTML数据解析
使用http://jsoup.org/ jsoup来解析HTML数据
http://www.open-open.com/jsoup/ jsoup中文手册
资料参考自:
http://stormzhang.github.io/android/2014/05/22/android-gson/