標籤:
package com.example.administrator.test.Fragment.Login;import android.support.v4.media.VolumeProviderCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import com.example.administrator.test.R;import com.google.gson.Gson;import com.google.gson.JsonArray;import com.google.gson.reflect.TypeToken;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.List;public class TestActivity4 extends AppCompatActivity { EditText et_3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test4); et_3=(EditText)findViewById(R.id.et_3); } //原生 public void bt1_OnClick(View v) throws JSONException { String strJson = "{\"id\":1,\"name\":\"大蝦\", " + "\"price\":12.3, " + "\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; //從Json字串轉成對象 JSONObject jo = new JSONObject(strJson); int id=jo.getInt("id"); String name=jo.getString("name"); double price=jo.getDouble("price"); String imagePath=jo.getString("imagePath"); ShopInfo shopInfo=new ShopInfo(id,name,price,imagePath); et_3.setText(shopInfo.toString() ); } //Gson public void bt5_OnClick(View v) { //Gson方式吧Json字串轉成對象 String strJson = "{\"id\":1,\"name\":\"大蝦\", " + "\"price\":12.3, " + "\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; ShopInfo shopInfo= new Gson().fromJson(strJson,ShopInfo.class);//反射 et_3.setText(shopInfo.toString()); } public void bt2_OnClick(View v) { String strJson = "{\"id\":1,\"name\":\"大蝦\", " + "\"price\":12.3, " + "\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; ShopInfo shopInfo= new Gson().fromJson(strJson,ShopInfo.class);//反射 et_3.setText(shopInfo.toString()); //把對象轉為Json格式字串 shopInfo.setName("龍蝦"); String str1=new Gson().toJson(shopInfo); et_3.setText(str1); } //原生方式把Json字串轉成集合 public void bt3_OnClick(View v) { //轉集合 String strjson="[{\"id\":1,\"name\":\"大蝦1\",\"price\":12.3," + " \"imagePath\":\"http://192.168.10.165:8080/f1.jpg\"}," + "{\"id\":2, \"name\":\"大蝦2\", \"price\":12.5, " + "\"imagePath\":\"http://192.168.10.165:8080/f2.jpg\"}]"; try { JSONArray jsonArray=new JSONArray(strjson); String str=""; for (int i=0;i<jsonArray.length();i++) { JSONObject jo=jsonArray.getJSONObject(i); int id=jo.getInt("id"); String name=jo.getString("name"); double price=jo.getDouble("price"); String imagePath=jo.getString("imagePath"); ShopInfo shopInfo=new ShopInfo(id,name,price,imagePath); str+=shopInfo.toString(); et_3.setText( str ); } }catch (Exception e) { e.printStackTrace(); } } //Gson把Json字串轉成集合 public void bt4_OnClick(View v) { String strjson="[{\"id\":1,\"name\":\"大蝦1\",\"price\":12.3," + " \"imagePath\":\"http://192.168.10.165:8080/f1.jpg\"}," + "{\"id\":2, \"name\":\"大蝦2\", \"price\":12.5, " + "\"imagePath\":\"http://192.168.10.165:8080/f2.jpg\"}]"; List<ShopInfo> list=new Gson().fromJson( strjson,new TypeToken<List<ShopInfo>>(){}.getType()); et_3.setText( list.toString() ); } //Gson把集合轉成Json字串 public void bt6_OnClick(View v) { String strjson="[{\"id\":1,\"name\":\"大蝦1\",\"price\":12.3," + " \"imagePath\":\"http://192.168.10.165:8080/f1.jpg\"}," + "{\"id\":2, \"name\":\"大蝦2\", \"price\":12.5, " + "\"imagePath\":\"http://192.168.10.165:8080/f2.jpg\"}]"; List<ShopInfo> list=new Gson().fromJson( strjson,new TypeToken<List<ShopInfo>>(){}.getType()); list.add( new ShopInfo(3,"龍蝦3",123.45,"lalallalalal")); String str1=new Gson().toJson( list ); et_3.setText( str1 ); }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Android原生方式把Json字串轉成對象" android:onClick="bt1_OnClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Gson方式把Json字串轉成對象" android:onClick="bt5_OnClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Gson方式把對象轉成Json格式" android:onClick="bt2_OnClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Android原生把Json格式數組轉成對象集合" android:onClick="bt3_OnClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Gson方式把Json格式數組轉成對象集合" android:onClick="bt4_OnClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Gson方式把對象集合轉成Json格式" android:onClick="bt6_OnClick" /> <EditText android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/et_3"/></LinearLayout>
View Code
Android原生方式解析Json,Gson三方架構方式解析Json