Android 一個類實現的 Json 解析 - 範型的有效利用,使其得以簡化

來源:互聯網
上載者:User

這是在一家工作過四個月的比利時公司時寫的。

當時忙於戶口調動的事兒,僅做了兩個項目,就離開了。

還是很感謝這家公司,戶口的調動除了我的中級職稱,也得到了這家公司老總的全力支援,遺憾沒能給予她更多的回報。

再次感謝謝。


這裡先挖個坑,後續有時間再填!


開始填坑:

package com.dday.dataaccesslayer.utils.helper;import java.io.FileNotFoundException;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Date;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import com.dday.dataaccesslayer.utils.StorageServiceFactory;import com.google.gson.FieldNamingPolicy;import com.google.gson.Gson;import com.google.gson.GsonBuilder;public class JsonHelper {public static  T getEntityFromJson(String fromJsonStr, Class classOfT) throws JSONException {GsonBuilder gsonb = new GsonBuilder();gsonb.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);DateDeserializer ds = new DateDeserializer();gsonb.registerTypeAdapter(Date.class, ds);Gson gson = gsonb.create();JSONObject j = new JSONObject(fromJsonStr);return gson.fromJson(j.toString(), classOfT);}public static  T getEntityFromInterStorage(Context context, String jsonFilePath, String charset, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = InterStorageHelper.readInternal(context, jsonFilePath, charset);return getEntityFromJson(jsonContent, classOfT);}public static  T getEntityFromInterStorage(Context context, String jsonFilePath, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {return getEntityFromInterStorage(context, jsonFilePath, "UTF-8", classOfT);}public static  T getEntityFromExterPrivateStorage(Context context, String jsonFilePath, String charset, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = StorageServiceFactory.getSharedInstance().getExterStorageService(context).getPrivateFile(jsonFilePath, charset);return getEntityFromJson(jsonContent, classOfT);}public static  T getEntityFromExterPrivateStorage(Context context, String jsonFilePath, Class classOfT) throws JSONException, FileNotFoundException, UnsupportedEncodingException, IOException {String jsonContent = StorageServiceFactory.getSharedInstance().getExterStorageService(context).getPrivateFile(jsonFilePath, "UTF-8");return getEntityFromJson(jsonContent, classOfT);}}



此類用到了GSON庫。

其中有一些其它相互關聯類型,這裡不便提供,僅供大家參考其中的邏輯。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.