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;  
    }  

    @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());

		}

	}

}

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...