1. 程式人生 > >android json原生解析

android json原生解析

//json格式的字串{}轉換成java物件
private void jsonToJavaObjectByNative() {
    //獲取或者建立json資料
String json = "{\n" +
            "\t\"id\":2,\"name\":\"大蝦\",\n"+
            "\t\"price\":12.3,\n"+
            "\t\"imagePath\":\"http://192.168.0.1:8080/yht.jpg\"\n"+
            "}\n";
    //解析json
try {
        JSONObject jsonObject = new 
JSONObject(json); int id = jsonObject.getInt("id"); int id1 = jsonObject.optInt("id"); String name = jsonObject.optString("name"); double price = jsonObject.optDouble("price"); String imagePath = jsonObject.optString("imagePath"); shopInfo = new ShopInfo(id,name,price,imagePath); } catch
(JSONException e) { e.printStackTrace(); } //顯示資料 tv_native_orignal.setText(json); tv_native_last.setText(shopInfo.toString()); }
List<ShopInfo> shopInfos = new ArrayList<>();
//json格式的字串[] 轉換為java物件的List
private void jsonToJavaListByNative() {
    //獲取或者建立json資料
String json = "[
\n" + "\t{\n" + "\t\t\"id\":1,\n" + "\t\t\"imagePath\":\"http://192.168.0.1:8080/test/f1.jpg\",\n" + "\t\t\"name\":\"袁合濤1\",\n" + "\t\t\"price\":\"12.3\"\n" + "\t},\n" + "\t{\n" + "\t\t\"id\":2,\n" + "\t\t\"imagePath\":\"http://192.168.0.1:8080/test/f2.jpg\",\n" + "\t\t\"name\":\"袁合濤2\",\n" + "\t\t\"price\":\"12.5\"\n" + "\t}\n" + "]"; //解析json資料 try { JSONArray jsonArray = new JSONArray(json); for(int i = 0;i < jsonArray.length();i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); if(null != jsonObject){ int id = jsonObject.getInt("id"); String name = jsonObject.optString("name"); double price = jsonObject.optDouble("price"); String imagePath = jsonObject.optString("imagePath"); ShopInfo shopInfo = new ShopInfo(id,name,price,imagePath); shopInfos.add(shopInfo); } } } catch (JSONException e) { e.printStackTrace(); } //顯示json tv_native_orignal.setText(json); tv_native_last.setText(shopInfos.toString()); }

解析複雜的json資料,需要一層一層的進行解析。

   JSONObject jsonObject = new JSONObject(json);

        //第一層解析
JSONObject data = jsonObject.optJSONObject("data");
        String  retcode = jsonObject.optString("retcode");

        //第二次解析
String countcommenturl = data.optString("countcommenturl");
        String more = data.optString("more");
        JSONArray news = data.optJSONArray("news");

        //第三次解析
for(int i = 0;i<news.length();i++){
            JSONObject jsonObject1 = news.optJSONObject(i);
            if(null != jsonObject1){
                int id = jsonObject1.optInt("id");
                String title = jsonObject1.optString("title");
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}


//json格式的字串{}轉換為java物件
private void jsonToJavaObjectByGson() {
    //獲取或建立json資料
String json = "{\n" +
            "    \"id\": 1,\n" +
            "    \"imagePath\": \"http://192.168.0.1:8080/test/f1.jpg\",\n" +
            "    \"name\": \"袁合濤1\",\n" +
            "    \"price\": \"12.3\"\n" +
            "}";
    //開始解析
Gson gson = new Gson();
    ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
    //顯示json資料
tv_gson_orignal.setText(json);
    tv_gson_last.setText(shopInfo.toString());
}
//json格式的字串[]轉換為java物件的List
private void jsonToJavaListByGson() {
    //建立或獲取json資料
String json  = "[\n" +
            "    {\n" +
            "        \"id\": 1,\n" +
            "        \"imagePath\": \"http://192.168.0.1:8080/test/f1.jpg\",\n" +
            "        \"name\": \"袁合濤1\",\n" +
            "        \"price\": \"12.3\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"id\": 2,\n" +
            "        \"imagePath\": \"http://192.168.0.1:8080/test/f2.jpg\",\n" +
            "        \"name\": \"袁合濤2\",\n" +
            "        \"price\": \"12.5\"\n" +
            "    }\n" +
            "]";
    //解析json資料
Gson gson = new Gson();
    List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() {
    }.getType());

    //資料展示
tv_gson_orignal.setText(json);
    tv_gson_last.setText(shops.toString());
}
//java物件轉換為json字串[]
private void javaToJsonArrayByGson() {
    //獲取或建立java物件
List<ShopInfo> shops = new ArrayList<>();
    ShopInfo baoyu = new ShopInfo(1, "鮑魚", 250.3, "baoyu");
    ShopInfo longixa = new ShopInfo(1, "龍蝦", 251.3, "longixa");
    shops.add(baoyu);
    shops.add(longixa);
    //生成json資料
Gson gson = new Gson();
    String json = gson.toJson(shops);

    //展示資料
tv_gson_orignal.setText(shops.toString());
    tv_gson_last.setText(json);
}

//java物件轉換為json字串{}
private void javaToGsonObjectByGson() {
    //獲取或建立java物件
ShopInfo shopInfo = new ShopInfo(1,"鮑魚",250.2,"baoyu");
    //生成json資料
Gson gson = new Gson();
    String json = gson.toJson(shopInfo);

    //展示資料
tv_gson_orignal.setText(shopInfo.toString());
    tv_gson_last.setText(json);
}

FastJson

//json格式的字串{}轉換為java物件
private void jsonToJavaObjectByFastJson() {
    //獲取或建立json資料
String json = "{\n" +
            "    \"id\": 1,\n" +
            "    \"imagePath\": \"http://192.168.0.1:8080/test/f1.jpg\",\n" +
            "    \"name\": \"袁合濤1\",\n" +
            "    \"price\": \"12.3\"\n" +
            "}";
    //解析json資料
ShopInfo shopInfo = JSON.parseObject(json,ShopInfo.class);

    //顯示資料
tv_fastjson_orignal.setText(json);
    tv_fastjson_last.setText(shopInfo.toString());
}
//json格式的字串[]轉換為java物件的List
private void jsonToJavaListByFastJson() {
    //建立或獲取json資料
String json  = "[\n" +
            "    {\n" +
            "        \"id\": 1,\n" +
            "        \"imagePath\": \"http://192.168.0.1:8080/test/f1.jpg\",\n" +
            "        \"name\": \"袁合濤1\",\n" +
            "        \"price\": \"12.3\"\n" +
            "    },\n" +
            "    {\n" +
            "        \"id\": 2,\n" +
            "        \"imagePath\": \"http://192.168.0.1:8080/test/f2.jpg\",\n" +
            "        \"name\": \"袁合濤2\",\n" +
            "        \"price\": \"12.5\"\n" +
            "    }\n" +
            "]";
    //解析JSon資料
List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);
    //顯示資料
tv_fastjson_orignal.setText(json);
    tv_fastjson_last.setText(shopInfos.toString());
}
//java物件轉換為json字串[]
private void javaToJsonArrayByFastJson() {
    //建立一個java集合
List<ShopInfo> shops = new ArrayList<>();
    ShopInfo yht = new ShopInfo(1, "yht", 25.3, "jj");
    ShopInfo yyy = new ShopInfo(2, "yyy", 25.3, "yyyyy");
    shops.add(yht);
    shops.add(yyy);
    //生成json資料
String jsonString = JSON.toJSONString(shops);
    //展示資料
tv_fastjson_orignal.setText(shops.toString());
    tv_fastjson_last.setText(jsonString);
}

//java物件轉換為json字串{}
private void javaToJsonObectByFastJson() {
    //建立一個java物件
ShopInfo shopInfo = new ShopInfo(1,"合濤",320.2,"hetao");
    //java轉換為json
String json = JSON.toJSONString(shopInfo);
    //顯示資料
tv_fastjson_orignal.setText(shopInfo.toString());
    tv_fastjson_last.setText(json);
}