json.jar地址下载 点击打开链接
+++++++++++++++++++++++++++++先看fastjson的使用方法++++++++++++++++++++++++++++++++
Student
package com.example.fastjson; public class Student { private String name; private int age; /** * 默认的构造方法必须要写,不然是无法解析的 */ public Student() { } public Student(String name,int age){ this.name = name; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student [name=" + name + ",age=" + age + "]"; } }
Grade
package com.example.fastjson; import java.util.ArrayList; import java.util.List; public class Grade { private String gradeName; private List<Student> students = new ArrayList<Student>(); public Grade() { } public Grade(String gradeName) { this.gradeName = gradeName; } public String getGradeName() { return gradeName; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
MainActivity
package com.example.fastjson; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 1、1输出javaBean的json格式 */ Student student = new Student("dandy",27); String jsonStudent1 = JSON.toJSONString(student); String jsonStudent2 = JSON.toJSONString(student,true); //jsonStudent:{"age":27,"name":"dandy"} Log.i("TAG","jsonStudent1 standard:"+jsonStudent1); Log.i("TAG","jsonStudent2 true:"+jsonStudent2); /** * 1、2输出javaBean的特定属性json格式 * new SimplePropertyPreFilter(实体类.class,"字段一","字段二"); */ SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class,"name"); String jsonStudent = JSON.toJSONString(student,filter); Log.i("TAG","jsonStudent filter:"+jsonStudent); /** * 1、3输出复杂的javaBean(带集合的) */ Grade grade = new Grade("高一"); grade.getStudents().add(new Student("dandy",27)); grade.getStudents().add(new Student("Tabolt",28)); String jsonGrade = JSON.toJSONString(grade); Log.i("TAG","jsonGrade:"+jsonGrade); /** * 2、1Json生成javaBean对象实例 */ String json = JSON.toJSONString(new Student("Tabolt",28)); Student studentBean = JSON.parSEObject(json,Student.class); Log.i("TAG","studentBean:"+studentBean); /** * 2、2Json生成javaBean对象实例 集合 */ List<Student> students = new ArrayList<Student>(); for(int i = 0;i < 5;i++){ Student studentFor = new Student("dandy_"+i,26+i); students.add(studentFor); } String jsonFor = JSON.toJSONString(students); List<Student> stus1 = JSON.parseArray(jsonFor,Student.class); List<Student> stus2 = JSON.parSEObject(jsonFor,new TypeReference<List<Student>>(){}); for(Student stu:stus1){ Log.i("TAG","studentBean:"+stu.toString()); } for(Student stu:stus2){ Log.i("TAG","studentBean:"+stu.toString()); } } }
log输出如下。。。
+++++++++++++++++++++++++++++先看Gson的使用方法++++++++++++++++++++++++++++++++
package com.example.gsondemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.Typetoken; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 1、1gson解析对象 */ Gson gson = new Gson(); Student student = new Student("dandy",27); String stuStr = gson.toJson(student); Log.i("TAG","str:" + stuStr); /** * 1、2gson解析数组对象 */ Grade grade = new Grade("高一"); grade.getStudents().add(new Student("dandy",27)); grade.getStudents().add(new Student("Tabolt",28)); String str = gson.toJson(grade); Log.i("TAG","str:" + str); /** * * 2、1 gson解析字符串为javaBean对象 */ Student fromJson2Stu = gson.fromJson(stuStr,Student.class); Log.i("TAG","fromJson2Stu-->" + fromJson2Stu.toString()); /** * 2、2gson解析字符串为javaBean对象集合 */ List<Student> students = new ArrayList<Student>(); for (int i = 0; i < 5; i++) { Student studentFor = new Student("dandy_" + i,26 + i); students.add(studentFor); } String jsonFor = gson.toJson(students); List<Student> fromJson2StuArr = gson.fromJson(jsonFor,new Typetoken<List<Student>>() { }.getType()); for (int i = 0; i < fromJson2StuArr.size(); i++) { Student student2 = fromJson2StuArr.get(i); Log.i("TAG","student2-->" + student2.toString()); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。