本文主要是介绍fastjson--Gson生成Json字符串,并且解析Json字符串生成对象,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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; } @Overridepublic 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 {@Overrideprotected 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 {@Overrideprotected 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());}}}
这篇关于fastjson--Gson生成Json字符串,并且解析Json字符串生成对象的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!