import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.json.JSONObject; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Iterator; import java.util.List;
public void toTest(View view) { //TestGeneric<String> t = new TestGeneric<String>(); ArrayList<String> arrayList = new ArrayList<String>(); //arrayList.setValue("Alo"); arrayList.add("123,456"); arrayList.add("789,abc"); Type type = new TypeToken<ArrayList<String>>(){}.getType(); Gson gson = new Gson(); String json = gson.toJson(arrayList, type); //String gStr = GsonUtils.gson.toJson(t,type); System.out.println(json); ArrayList<String> t1 = gson.fromJson(json, type); Iterator it1 = arrayList.iterator(); while(it1.hasNext()){ System.out.println(it1.next()); } for(Iterator it2 = arrayList.iterator();it2.hasNext();){ System.out.println(it2.next()); } for(String tmp:arrayList){ System.out.println(tmp); } //方法4 for(int i = 0;i < arrayList.size(); i ++){ System.out.println(arrayList.get(i)); } System.out.println(t1.get(0)); System.out.println(t1.get(1));}