<span style="font-size:18px;">
</span>
<span style="font-size:18px;"></span><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 22px; font-family: 宋体, Arial, sans-serif; font-size: 14px; line-height: 25px; background-color: rgb(239, 244, 250);">有一种叫做JSON (JavaScript Object Notation) 的轻量级数据交换格式能够替代XML的工作.<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" /><span style="padding: 0px; margin: 0px;">优点:</span><br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />1. 数据格式比较简单, 易于读写, 格式都是压缩的, 占用带宽小<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />2. 易于解析这种语言, 客户端JavaScript可以简单的通过eval()进行JSON数据的读取<br style="padding: 0px; margin: 0px;" /><br style="padding: 0px; margin: 0px;" />3. 支持多种语言, 包括ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby等语言服务器端语言, 便于服务器端的解析</p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 22px; font-family: 宋体, Arial, sans-serif; font-size: 14px; line-height: 25px; background-color: rgb(239, 244, 250);">4. 在PHP世界, 已经有PHP-JSON和JSON-PHP出现了, 便于PHP序列化后的程序直接调用. PHP服务器端的对象、数组等能够直接生JSON格式, 便于客户端的访问提取. 另外PHP的PEAR类已经提出了支持 (http://pear.php.net/pepr/pepr-proposal-show.php?id=198)</p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 22px; font-family: 宋体, Arial, sans-serif; font-size: 14px; line-height: 25px; background-color: rgb(239, 244, 250);">5. 因为JSON格式能够直接为服务器端代码使用, 大大简化了服务器端和客户端的代码开发量, 但是完成的任务不变, 且易于维护缺点:1. 没有XML格式这么推广的深入人心和使用广泛, 没有XML那么通用性2. JSON格式目前在Web Service中推广还属于初级阶段</p><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> ◆可读性</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> JSON和XML的可读性可谓不相上下,一边是建议的语法,一边是规范的标签形式,很难分出胜负。</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> ◆可扩展性</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> XML天生有很好的扩展性,JSON当然也有。</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> ◆编码难度</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> XML有丰富的编码工具,比如Dom4j、JDom等,JSON也有json.org提供的工具,但是JSON的编码明显比XML容易许多,即使不借助工具也能写出JSON的代码,可是要写好XML就不太容易了。</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> ◆解码难度</span><br style="box-sizing: content-box; color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);" /><span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);"> XML的解析得考虑子节点父节点,让人头昏眼花,而JSON的解析难度几乎为0。这一点XML输的真是没话说。</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">JavaBean</span>
<span style="font-size:18px;"></span><pre name="code" class="java">public class Student {
private String name;
private String birthday;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, String birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
<span style="white-space:pre"> </span>public String toString() {
<span style="white-space:pre"> </span>return "Student [name=" + name + ", birthday=" + birthday + "]";
<span style="white-space:pre"> </span>}
}
public class MainActivity extends Activity {
private static final String TAG = "Activity";
private Gson gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tView_j= (TextView) findViewById(R.id.tv_json);
TextView tView_g= (TextView) findViewById(R.id.tv_gson);
TextView tView= (TextView) findViewById(R.id.tv_listgson);
//自己把数据封装成json格式
Student student = new Student("lmz", "22");
gson = new Gson();
String json = gson.toJson(student);
tView_j.setText("json: "+json);
//自己把json数据封装成解析成实体对象
String stu_json=" {'name':'lmz','birthday':22} ";
Student stud = gson.fromJson(stu_json, Student.class);
tView_g.setText("gson: "+stud.getName()+"...."+stud.getBirthday());
//带泛型的List之间的转化 实体---(gson)-->json--(gson)-->实体
List<Student> list = new ArrayList<Student>();
Student student1 = new Student("cjk", "26");
Student student2 = new Student("cgx", "28");
Student student3 = new Student("xzmly", "24");
list.add(student1);
list.add(student2);
list.add(student3);
String jsons = gson.toJson(list);//{.......}
//Type listType=new TypeToken<ArrayList<Student>>(){}.getType();//TypeToken内的泛型就是Json数据
Type type = new TypeToken<ArrayList<Student>>(){}.getType();
List<Student> studs = gson.fromJson(jsons, type);
for (Student stu : studs) {
//显示数据
tView.setText("student: "+stu);
}
}
}
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>