package com.example.volleylearn; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.test.AndroidTestCase; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; /* 1. 將json格式的字符串{}轉換爲Java對象, 使用原生API 2. 將json格式的字符串{}轉換爲Java對象, 使用GSON 3. 將json格式的字符串[]轉換爲Java對象的List, 使用原生API 4. 將json格式的字符串[]轉換爲Java對象的List, 使用GSON 5. 將Java對象轉換爲json字符串{}, 使用GSON 6. 將Java對象的List轉換爲json字符串[], 使用GSON */ public class JsonTest extends AndroidTestCase{ /* * 1. 將json格式的字符串{}轉換爲Java對象, 使用原生API */ public void testJsonToObject() throws JSONException { String jsonString = "{\"id\":2, \"name\":\"大蝦\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; //將json字符串封裝爲JSONObject對象 JSONObject jsonObject = new JSONObject(jsonString); //從對象中根據key獲得對應的value int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); double price = jsonObject.getDouble("price"); String imagePath = jsonObject.getString("imagePath"); //封裝ShopInfo對象 ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath); Log.e("TAG", shopInfo.toString()); } /* * 1. 將json格式的字符串{}轉換爲Java對象, 使用GSON */ public void testJsonToObject2() { String jsonString = "{\"id\":3, \"name\":\"大蝦\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}"; ShopInfo shopInfo = new Gson().fromJson(jsonString, ShopInfo.class); Log.e("TAG", shopInfo.toString()); } /* * 3. 將json格式的字符串[]轉換爲Java對象的List, 使用原生API */ public void testJsonToList() throws JSONException { String jsonString = "[{\"id\":3, \"name\":\"大蝦\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}," + "{\"id\":5, \"name\":\"大蝦2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]"; List<ShopInfo> list = new ArrayList<ShopInfo>(); //1. 將json字符串包裝JSONArray對象 JSONArray jsonArray = new JSONArray(jsonString); //2. 遍歷JSONArray對象全部元素(JSONObject), 並將每一個元素封裝爲shopInfo, 並添加到List for(int i=0;i<jsonArray.length();i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); //從對象中根據key獲得對應的value int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); double price = jsonObject.getDouble("price"); String imagePath = jsonObject.getString("imagePath"); //封裝ShopInfo對象 ShopInfo shopInfo = new ShopInfo(id, name, price, imagePath); list.add(shopInfo); } Log.e("TAG", list.toString()); } /* * 4. 將json格式的字符串[]轉換爲Java對象的List, 使用GSON */ public void testJsonToList2() throws JSONException { String jsonString = "[{\"id\":4, \"name\":\"大蝦\", \"price\":12.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"}," + "{\"id\":6, \"name\":\"大蝦2\", \"price\":128.3,\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f2.jpg\"}]"; List<ShopInfo> list = new Gson().fromJson(jsonString, new TypeToken<List<ShopInfo>>(){}.getType()); Log.e("TAG", list.toString()); } /* 5. 將Java對象轉換爲json字符串{}, 使用GSON */ public void testObjectToJson() { ShopInfo info = new ShopInfo(3, "KK", 1000, "http://www.sina.com"); String json = new Gson().toJson(info); Log.e("TAG", json); } /* 6. 將Java對象的List轉換爲json字符串[], 使用GSON */ public void testListToJson() { List<ShopInfo> list = new ArrayList<ShopInfo>(); list.add(new ShopInfo(3, "KK", 1000, "http://www.sina.com")); list.add(new ShopInfo(4, "KK2", 2000, "http://www.sina.com222")); String json = new Gson().toJson(list); Log.e("TAG", json); } public void testJsonToMap() { String jsonString = "{\"my name\":\"大蝦\", \"1\":12}"; Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<Map<String, Object>>(){}.getType()); Log.e("TAG", map.toString()); } }
FastJson // (4)將Java對象的List轉換爲json字符串[] private void javaToJsonArrayByFastJson() { // 1 建立一個Java集合 List<ShopInfo> shops = new ArrayList<>(); ShopInfo baoyu = new ShopInfo(1, "鮑魚", 250.0, "baoyu"); ShopInfo longxia = new ShopInfo(2, "龍蝦", 251.0, "longxia"); shops.add(baoyu); shops.add(longxia); // 2 生成JSON數據 String json = JSON.toJSONString(shops); } // (3)將Java對象轉換爲json字符串{} private void javaToJsonObjectByFastJson() { // 1 建立一個Java對象 ShopInfo shopInfo = new ShopInfo(1, "鮑魚", 250.0, "baoyu"); // 2 生成JSON數據 String json = JSON.toJSONString(shopInfo); } // (2)將json格式的字符串[]轉換爲Java對象的List private void jsonToJavaListByFastJson() { // 1 獲取或建立json數據 String json = "[\n" + " {\n" + " \"id\": 1,\n" + " \"imagePath\": \"http://192.168.10.165:8080/f1.jpg\",\n" + " \"name\": \"大蝦1\",\n" + " \"price\": 12.3\n" + " },\n" + " {\n" + " \"id\": 2,\n" + " \"imagePath\": \"http://192.168.10.165:8080/f2.jpg\",\n" + " \"name\": \"大蝦2\",\n" + " \"price\": 12.5\n" + " }\n" + "]"; // 2 解析JSON數據 List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class); } // (1)將json格式的字符串{}轉換爲Java對象 private void jsonToJavaObjectByFastJson() { // 1 獲取或建立json數據 String json = "{\n" + "\t\"id\":2, \"name\":\"大蝦\", \n" + "\t\"price\":12.3, \n" + "\t\"imagePath\":\"http://192.168.10.165:8080/L05_Server/images/f1.jpg\"\n" + "}\n"; // 2 解析JSON數據 ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class); } }