标签:
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>
Android原生方式解析Json,Gson三方框架方式解析Json
标签:
原文地址:http://www.cnblogs.com/jiang2538406936/p/5726397.html