关闭

fastjson--Gson生成Json字符串,并且解析Json字符串生成对象

1546人阅读 评论(0) 收藏 举报
分类:

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

		}

	}

}






2
0
查看评论

Gson在java对象和json字符串之间转换

GsonLib下载链接 Gson相比org.json最大的好处是从json字符串转向java实例时候少了依据每个实例自己赋值的过程,比如在org.json的时候,我们先从json字符串构建一个jsonobject,然后用各种的json get方法获取到每个字段的值,这是一个体力活。而gson依据反...
  • JQ_AK47
  • JQ_AK47
  • 2016-09-27 22:34
  • 25255

gson实现字符串与对象的相互转换

最近一个项目需要用到JSON,需要在JSON字符串和对象之间相互转换,在网上找了些资料,发现google的Gson还是比较不错的。 废话不说,下面是简单的例子: 先上源码:下载(包含jar包) Person实体类[java] view plain copy print?package co...
  • android_zhengyongbo
  • android_zhengyongbo
  • 2017-03-28 12:48
  • 4060

Android解析Json字符串和生成json字符串

直接写例子说明最清晰,使用google的GSON来搞定 1. 要被解析生成json字符串和从json字符串中获取生成的对象的类 package com.habby.test.test1; class Person { int age; String name; Location[] loca...
  • linux_Mary_me
  • linux_Mary_me
  • 2014-03-22 16:22
  • 1283

Android中Gson解析和生成JSON字符串讲解,GsonFormat一键生成对应的数据对象

工作内容: 1.如何Gson解析JSON字符串讲解。 2.LruCache缓存机制的讲解。 学习分享: 一、Gson解析JSON字符串 1.需明确Gson解析JSON字符串与使用JSONObject,JSONArray解析JSON字符串一样,需要逐层解析。 2.Gson的优势在于,可以轻松将JSON...
  • yingtian648
  • yingtian648
  • 2016-09-12 21:49
  • 2045

JsonObject生成Json字符串,并且解析为对象-----JsonReader解析Json字符串

MainActivity package com.example.gsondemo; import java.io.IOException; import org.json.JSONArray; import org.json.JSONException; import org.json.JS...
  • u013210620
  • u013210620
  • 2016-05-31 15:42
  • 3538

使用python解析Json字符串-获取Json字符串关键字

import json data = { "statusCode": 200, "data": { "totoal": "5", "height": "5.97", ...
  • kaka735
  • kaka735
  • 2015-05-26 16:39
  • 4440

Jackson解析与生成Json字符串

Jackson API:http://tool.oschina.net/apidocs/apidoc?api=jackson-1.9.9 流式API读取和写入JSON内容离散事件。 JsonParser读取数据,而JsonGenerator写入数据。它是三者中最有效的方法,是最低开销和最快的读/写...
  • WangQYoho
  • WangQYoho
  • 2016-09-19 11:25
  • 3157

Java解析Json字符串--复杂对象

Json示例:{ "name": "三班", "students": [ { "age": 25, "gender": ...
  • daidaineteasy
  • daidaineteasy
  • 2015-12-22 18:31
  • 5395

java解析json字符串的两种方法详解(Android通用)

java解析json字符串的两种方法详解(Android通用)
  • canot
  • canot
  • 2016-01-23 20:20
  • 9991

js怎么解析json格式字符串

json是种常用数据传输格式,js怎么解析json格式的数据呢,请参阅下面的代码: var data = '{"return_code":0,"return_message":"success",&q...
  • love__coder
  • love__coder
  • 2011-07-19 17:03
  • 53000