使用Gson将Java对象转换为Json

Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。

Gson里最重要的对象有2个Gson 和GsonBuilder。

Gson有2个最基本的方法
1) toJson() – 转换java 对象到JSON
2) fromJson() – 转换JSON到java对象

注意使用Gson时记得添加第三方类库gson-1.6.jar

    

main.xml布局文件

复制代码
    
    
    
    
<? xml version = " 1.0 " encoding = " utf-8 " ?> < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android " android:orientation = " vertical " android:layout_width = " fill_parent " android:layout_height = " fill_parent " > < Button android:id = " @+id/btn01 " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:gravity = " center_horizontal " android:text = " 开始解析01 " /> < Button android:id = " @+id/btn02 " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:gravity = " center_horizontal " android:text = " 开始解析02 " /> < Button android:id = " @+id/btn03 " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:gravity = " center_horizontal " android:text = " 开始解析03 " /> < Button android:id = " @+id/btn04 " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:gravity = " center_horizontal " android:text = " JavaToJson " /> < Button android:id = " @+id/btn05 " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:gravity = " center_horizontal " android:text = " 把对象的属性转换成json的名字 " /> </ LinearLayout >
复制代码

实体类User

复制代码
    
    
    
    
package com.ljq.json; import com.google.gson.annotations.SerializedName; public class User { // 将java对象的属性转换成指定的json名字 @SerializedName( " username " ) private String name; private int age; public User() { super (); } public User(String name, int age) { super (); this .name = name; this .age = age; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getAge() { return age; } public void setAge( int age) { this .age = age; } }
复制代码

工具类JsonUtils

复制代码
    
    
    
    
package com.ljq.json; import java.io.StringReader; import java.lang.reflect.Type; import java.util.Iterator; import java.util.LinkedList; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; public class JsonUtils { public void parseJson01(String jsonData){ try { // 解析json对象首先要生产一个JsonReader对象 JsonReader reader = new JsonReader( new StringReader(jsonData)); reader.beginArray(); while (reader.hasNext()){ reader.beginObject(); while (reader.hasNext()){ String tagName = reader.nextName(); if ( " name " .equals(tagName)){ System.out.println( " name---> " + reader.nextString()); } else if ( " age " .equals(tagName)){ System.out.println( " age---> " + reader.nextInt()); } } reader.endObject(); } reader.endArray(); } catch (Exception e) { e.printStackTrace(); } } public void parseJson02(String jsonData){ Gson gson = new Gson(); User user = gson.fromJson(jsonData, User. class ); System.out.println( " name---> " + user.getName()); System.out.println( " age----> " + user.getAge()); } public void parseJson03(String jsonData){ Type listType = new TypeToken < LinkedList < User >> (){}.getType(); Gson gson = new Gson(); LinkedList < User > users = gson.fromJson(jsonData, listType); for (User user:users){ System.out.println( " name---> " + user.getName()); System.out.println( " age----> " + user.getAge()); } System.out.println( " ================== " ); for (Iterator iterator = users.iterator(); iterator.hasNext();) { User user = (User) iterator.next(); System.out.println( " name---> " + user.getName()); System.out.println( " age----> " + user.getAge()); } } }
复制代码

JsonActivity

复制代码
    
    
    
    
package com.ljq.json; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class JsonActivity extends Activity { private Button btn01 = null ; private Button btn02 = null ; private Button btn03 = null ; private Button btn04 = null ; private Button btn05 = null ; private String jsonData01 = " [{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}] " ; private String jsonData02 = " {\"name\":\"Michael\",\"age\":20} " ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); btn01 = (Button)findViewById(R.id.btn01); btn02 = (Button)findViewById(R.id.btn02); btn03 = (Button)findViewById(R.id.btn03); btn04 = (Button)findViewById(R.id.btn04); btn05 = (Button)findViewById(R.id.btn05); btn01.setOnClickListener(listener); btn02.setOnClickListener(listener); btn03.setOnClickListener(listener); btn04.setOnClickListener(listener); btn05.setOnClickListener(listener); } View.OnClickListener listener = new View.OnClickListener(){ public void onClick(View v) { JsonUtils jsonUtils = new JsonUtils(); switch (v.getId()) { case R.id.btn01: jsonUtils.parseJson01(jsonData01); break ; case R.id.btn02: jsonUtils.parseJson02(jsonData02); break ; case R.id.btn03: jsonUtils.parseJson03(jsonData01); break ; case R.id.btn04: // 把java对象转换成json User user01 = new User( " linjiqin " , 24 ); Gson gson = new Gson(); String obj = gson.toJson(user01); System.out.println(obj); // 输出{"name":"linjiqin", "age":24} break ; case R.id.btn05: // 将java对象的属性转换成指定的json名字 User user02 = new User( " lintepeng " , 22 ); Gson gson2 = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); String obj2 = gson2.toJson(user02); System.out.println(obj2); // 输出{"username":"lintepeng","Age":22} break ; } } }; }

你可能感兴趣的:(使用Gson将Java对象转换为Json)