1. 程式人生 > >FastJson的範型解析相關的幾個demo

FastJson的範型解析相關的幾個demo

fastjson 的確很好用,官網:https://github.com/alibaba/fastjson/wiki/常見問題

但是在解析範型的時候要特別的小心,使用下面的goodExample的方法去解析是最好的

還有就是需要進行範型解析的類,一定不要包含內部了,否者會報錯


package com.example.kodulf.utilsdemo.utils.http.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.example.kodulf.utilsdemo.entity.City;
import com.example.kodulf.utilsdemo.entity.Flow;
import com.example.kodulf.utilsdemo.utils.http.Result;
import com.example.kodulf.utilsdemo.utils.http.ResultList;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kodulf on 2017/5/14.
 * FastJson的測試
 *
 * 為什麼要做這個測試,主要是為了解析範型的,因為專案中Result<T>是使用的範型,解析起來就需要特殊的處理new TypeReference()
 * 注意了,前面四個方法,怎麼說呢,都相當於已經制定了型別了,
 * 注意看兩個goodexample,還有就是showCastError
 * 千萬,千萬要記住,需要解析的類裡面不要使用內部類,否者fastjson 會報錯報錯create instance error, class com.example.kodulf.utilsdemo.entity.City$Flow
 */

public class FastJsonTest{


    //todo 千萬,千萬要記住,需要解析的類裡面不要使用內部類,否者fastjson 會報錯報錯create instance error, class com.example.kodulf.utilsdemo.entity.City$Flow

    public static void main(String[] args){

        //{"reason":"允許充值的手機號碼及金額","result":null,"error_code":0}
        //{"reason":"success","result":[{"city":"全國","company":"中國聯通","companytype":"1","name":"中國聯通全國流量套餐","type":"1","flows":[{"id":"34","p":"20M","v":"20","inprice":"2.880"},{"id":"1","p":"50M","v":"50","inprice":"5.760"},{"id":"35","p":"100M","v":"100","inprice":"9.600"},{"id":"2","p":"200M","v":"200","inprice":"14.400"},{"id":"36","p":"500M","v":"500","inprice":"28.800"},{"id":"37","p":"1G","v":"1024","inprice":"48.000"}]},{"city":"全國","company":"中國移動","companytype":"2","name":"中國移動全國流量套餐","type":"1","flows":[{"id":"3","p":"10M","v":"10","inprice":"2.985"},{"id":"4","p":"30M","v":"30","inprice":"4.975"},{"id":"5","p":"70M","v":"70","inprice":"9.950"},{"id":"49","p":"100M","v":"100","inprice":"9.950"},{"id":"6","p":"150M","v":"150","inprice":"19.900"},{"id":"50","p":"300M","v":"300","inprice":"19.900"},{"id":"7","p":"500M","v":"500","inprice":"29.850"},{"id":"26","p":"1G","v":"1024","inprice":"49.750"},{"id":"27","p":"2048M","v":"2048","inprice":"69.650"}]},{"city":"全國","company":"中國電信","companytype":"3","name":"中國電信全國流量套餐","type":"1","flows":[{"id":"8","p":"10M","v":"10","inprice":"1.860"},{"id":"9","p":"30M","v":"30","inprice":"4.650"},{"id":"32","p":"50M","v":"50","inprice":"6.510"},{"id":"10","p":"100M","v":"100","inprice":"9.300"},{"id":"11","p":"200M","v":"200","inprice":"13.950"},{"id":"12","p":"500M","v":"500","inprice":"27.900"},{"id":"28","p":"1G","v":"1024","inprice":"46.500"}]}],"error_code":0}

//        parseTestResultString();
//
//        parseTestResultCity();
//
//        parseTestResultListCityList();
//
//        parseTestResultCityList();

//        testError();

//        testError2();

        showCastError();

//        goodExampleToResultGeneric();

//        goodExampleToResultListGeneric();

    }

    public static void parseTestResultString(){

        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":\"hello world\",\"error_code\":0}";

        Result<String> result = (Result<String>)JSON.parseObject(jsonResult, new TypeReference<Result<String>>(){});

        System.out.println(result.toString());

    }

    public static void parseTestResultCity(){

        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},\"error_code\":0}";

        Result<City> result = (Result<City>)JSON.parseObject(jsonResult, new TypeReference<Result<City>>(){});

        System.out.println(result.getResult().getCity());

        ArrayList<Flow> flows = result.getResult().getFlows();
        System.out.println(flows.get(0).getP());

    }


    /**
     * 測試使用ResultList<City> 獲取citylist
     */
    public static void parseTestResultListCityList(){

        String jsonResultList = "{\"reason\":\"success\",\"result\":[{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},{\"city\":\"全國\",\"company\":\"中國移動\",\"companytype\":\"2\",\"name\":\"中國移動全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"3\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"2.985\"},{\"id\":\"4\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.975\"},{\"id\":\"5\",\"p\":\"70M\",\"v\":\"70\",\"inprice\":\"9.950\"},{\"id\":\"49\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.950\"},{\"id\":\"6\",\"p\":\"150M\",\"v\":\"150\",\"inprice\":\"19.900\"},{\"id\":\"50\",\"p\":\"300M\",\"v\":\"300\",\"inprice\":\"19.900\"},{\"id\":\"7\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"29.850\"},{\"id\":\"26\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"49.750\"},{\"id\":\"27\",\"p\":\"2048M\",\"v\":\"2048\",\"inprice\":\"69.650\"}]},{\"city\":\"全國\",\"company\":\"中國電信\",\"companytype\":\"3\",\"name\":\"中國電信全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"8\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"1.860\"},{\"id\":\"9\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.650\"},{\"id\":\"32\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"6.510\"},{\"id\":\"10\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.300\"},{\"id\":\"11\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"13.950\"},{\"id\":\"12\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"27.900\"},{\"id\":\"28\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"46.500\"}]}],\"error_code\":0}";

        ResultList<City> resultList = (ResultList<City>) JSON.parseObject(jsonResultList, new TypeReference<ResultList<City>>(){});

        System.out.println(resultList.toString());

        List<City> result1 = resultList.getResult();
        for (City city : result1) {
            System.out.println(city.getCity());
        }

    }

    /**
     * 測試解析裡面是ResultList,json 是CityList的
     */
    public static void parseTestResultCityList(){
        String jsonResultList = "{\"reason\":\"success\",\"result\":[{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},{\"city\":\"全國\",\"company\":\"中國移動\",\"companytype\":\"2\",\"name\":\"中國移動全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"3\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"2.985\"},{\"id\":\"4\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.975\"},{\"id\":\"5\",\"p\":\"70M\",\"v\":\"70\",\"inprice\":\"9.950\"},{\"id\":\"49\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.950\"},{\"id\":\"6\",\"p\":\"150M\",\"v\":\"150\",\"inprice\":\"19.900\"},{\"id\":\"50\",\"p\":\"300M\",\"v\":\"300\",\"inprice\":\"19.900\"},{\"id\":\"7\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"29.850\"},{\"id\":\"26\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"49.750\"},{\"id\":\"27\",\"p\":\"2048M\",\"v\":\"2048\",\"inprice\":\"69.650\"}]},{\"city\":\"全國\",\"company\":\"中國電信\",\"companytype\":\"3\",\"name\":\"中國電信全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"8\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"1.860\"},{\"id\":\"9\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.650\"},{\"id\":\"32\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"6.510\"},{\"id\":\"10\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.300\"},{\"id\":\"11\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"13.950\"},{\"id\":\"12\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"27.900\"},{\"id\":\"28\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"46.500\"}]}],\"error_code\":0}";

        Result<List<City>> listResult = JSON.parseObject(jsonResultList, new TypeReference<Result<List<City>>>() {
        });
        System.out.println(listResult.toString());

        List<City> result1 = (List<City>)listResult.getResult();
        for (City city : result1) {
            System.out.println(city.getCity());
        }
    }

    /**
     * 錯誤的復現
     */
    public static void testError(){
        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},\"error_code\":0}";


        Result<City> t=parseTest(jsonResult,new Result<City>(),new City());

        //只要在這裡是獲取範型裡面的東西的時候,例如這裡是City,獲取city的屬性,一定是會報錯的
        try {
            System.out.println(t.getResult().getCity());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 測試,這裡面 T ret = (T) tResult;應該是導致錯誤的地方
     * 多級範型,解析的時候報錯,
     * 參考一下:http://blog.csdn.net/lanmo555/article/details/52769372
     * 範型的
     * @param jsonString
     * @param t
     * @param k
     * @param <K>
     * @param <T>
     * @return
     */
    public static <K, T extends Result<K>> T parseTest(String jsonString,T t,final K k){
        Result<K> tResult = JSON.parseObject(jsonString, new TypeReference<Result<K>>() {
        });

        System.out.println(tResult.toString());
        T ret = (T) tResult;
        System.out.println(t.getReason());

        return ret;
    }


    /**
     * 測試Error2,
     *
     * 之前是再City裡面設定了一個內部類flow,如果這樣做的話,就會報錯create instance error, class com.example.kodulf.utilsdemo.entity.City$Flow
     *
     * 將這個內部類刪除了以後,重新建立一個類(不是City的內部類),就不會報錯了
     */
    public static void testError2(){
        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},\"error_code\":0}";


        Result<City> t= parseTestFail2(jsonResult,new Result<City>(),new City());

        try {
            System.out.println(t.getResult().getCity());
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static <K, T extends Result<K>> T parseTestFail2(String jsonString, T t, final K k){
        Result<K> responseObject = JSON.parseObject(jsonString, new TypeReference<Result<K>>() {
        });

        System.out.println(responseObject.toString());
        //修復的方法

                        if (responseObject.getResult() != null) {
                            K valueObject = null;
                            Class<?> targetType= k.getClass();
                            if (targetType == String.class) {
                                valueObject =  (K)responseObject.getResult().toString();
                            }
                            else if ((targetType == Integer.class) || (targetType == Integer.TYPE))
                                valueObject =  (K)Integer.valueOf(responseObject.getResult().toString());
                            else if ((targetType == Byte.class) || (targetType == Byte.TYPE))
                                valueObject =  (K)Byte.valueOf(responseObject.getResult().toString());
                            else if ((targetType == Double.class) || (targetType == Double.TYPE))
                                valueObject =  (K)Double.valueOf(responseObject.getResult().toString());
                            else if ((targetType == Float.class) || (targetType == Float.TYPE))
                                valueObject =  (K)Float.valueOf(responseObject.getResult().toString());
                            else if ((targetType == Long.class) || (targetType == Long.TYPE))
                                valueObject =  (K)Long.valueOf(responseObject.getResult().toString());
                            else if ((targetType == Short.class) || (targetType == Short.TYPE))
                                valueObject =  (K)Short.valueOf(responseObject.getResult().toString());
                            else if (targetType == BigDecimal.class )
                                valueObject =  (K)new BigDecimal(responseObject.getResult().toString());
                            else{
                                valueObject = (K) JSONObject.toJavaObject((JSONObject) responseObject.getResult(), k.getClass());
                            }
                            responseObject.setResult(valueObject);
                        }

        T ret = (T) responseObject;
        System.out.println(t.getReason());

        return ret;
    }

    /**
     * 簡單的復原CastError的error
     */
    public static void showCastError(){
        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},\"error_code\":0}";
        Result<City> result = new Result<>();
        showCastError(jsonResult,result);
        System.out.println(result.getResult().getCity());
    }


    public static <T> void showCastError(String jsonString, Result<T> result){
        Result<T> tResult = JSON.parseObject(jsonString, new TypeReference<Result<T>>() {
        });

        if(tResult.getResult()!=null){
            result.setResult(tResult.getResult());
        }
        result.setReason(tResult.getReason());
        result.setError_code(tResult.getError_code());
    }


    /**
     * 正確的解析韓歐範型型別的Result的方法的example
     */
    public static void goodExampleToResultGeneric(){
        String jsonResult = "{\"reason\":\"允許充值的手機號碼及金額\",\"result\":{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},\"error_code\":0}";

        Result<City> result = new Result<>();
        try {
            parseJsonStringToResultGeneric(result,jsonResult,new City());

            System.out.println(result.getResult().getCity());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 範型解析為特定類
     * @param result
     * @param responseStr
     * @param k
     * @param <K>
     * @throws Exception
     */
    public static <K> void parseJsonStringToResultGeneric(Result<K> result, String responseStr, K k) throws Exception{
        Result<K> responseObject = JSON.parseObject(responseStr, new TypeReference<Result<K>>() {
        });
        if (responseObject.getResult() != null) {
            K valueObject = null;
            Class<?> targetType = k.getClass();
            if (targetType == String.class) {
                valueObject = (K) responseObject.getResult().toString();
            } else if ((targetType == Integer.class) || (targetType == Integer.TYPE))
                valueObject = (K) Integer.valueOf(responseObject.getResult().toString());
            else if ((targetType == Byte.class) || (targetType == Byte.TYPE))
                valueObject = (K) Byte.valueOf(responseObject.getResult().toString());
            else if ((targetType == Double.class) || (targetType == Double.TYPE))
                valueObject = (K) Double.valueOf(responseObject.getResult().toString());
            else if ((targetType == Float.class) || (targetType == Float.TYPE))
                valueObject = (K) Float.valueOf(responseObject.getResult().toString());
            else if ((targetType == Long.class) || (targetType == Long.TYPE))
                valueObject = (K) Long.valueOf(responseObject.getResult().toString());
            else if ((targetType == Short.class) || (targetType == Short.TYPE))
                valueObject = (K) Short.valueOf(responseObject.getResult().toString());
            else if (targetType == BigDecimal.class)
                valueObject = (K) new BigDecimal(responseObject.getResult().toString());
            else {
                valueObject = (K) JSONObject.toJavaObject((JSONObject) responseObject.getResult(), k.getClass());
            }
            result.setResult(valueObject);
        }
        result.setError_code(responseObject.getError_code());
        result.setReason(responseObject.getReason());

    }


    /**
     * 正確的解析韓歐範型型別的ResultList的方法的example
     */
    public static void goodExampleToResultListGeneric(){
        String jsonResultList = "{\"reason\":\"success\",\"result\":[{\"city\":\"全國\",\"company\":\"中國聯通\",\"companytype\":\"1\",\"name\":\"中國聯通全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"34\",\"p\":\"20M\",\"v\":\"20\",\"inprice\":\"2.880\"},{\"id\":\"1\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"5.760\"},{\"id\":\"35\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.600\"},{\"id\":\"2\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"14.400\"},{\"id\":\"36\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"28.800\"},{\"id\":\"37\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"48.000\"}]},{\"city\":\"全國\",\"company\":\"中國移動\",\"companytype\":\"2\",\"name\":\"中國移動全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"3\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"2.985\"},{\"id\":\"4\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.975\"},{\"id\":\"5\",\"p\":\"70M\",\"v\":\"70\",\"inprice\":\"9.950\"},{\"id\":\"49\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.950\"},{\"id\":\"6\",\"p\":\"150M\",\"v\":\"150\",\"inprice\":\"19.900\"},{\"id\":\"50\",\"p\":\"300M\",\"v\":\"300\",\"inprice\":\"19.900\"},{\"id\":\"7\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"29.850\"},{\"id\":\"26\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"49.750\"},{\"id\":\"27\",\"p\":\"2048M\",\"v\":\"2048\",\"inprice\":\"69.650\"}]},{\"city\":\"全國\",\"company\":\"中國電信\",\"companytype\":\"3\",\"name\":\"中國電信全國流量套餐\",\"type\":\"1\",\"flows\":[{\"id\":\"8\",\"p\":\"10M\",\"v\":\"10\",\"inprice\":\"1.860\"},{\"id\":\"9\",\"p\":\"30M\",\"v\":\"30\",\"inprice\":\"4.650\"},{\"id\":\"32\",\"p\":\"50M\",\"v\":\"50\",\"inprice\":\"6.510\"},{\"id\":\"10\",\"p\":\"100M\",\"v\":\"100\",\"inprice\":\"9.300\"},{\"id\":\"11\",\"p\":\"200M\",\"v\":\"200\",\"inprice\":\"13.950\"},{\"id\":\"12\",\"p\":\"500M\",\"v\":\"500\",\"inprice\":\"27.900\"},{\"id\":\"28\",\"p\":\"1G\",\"v\":\"1024\",\"inprice\":\"46.500\"}]}],\"error_code\":0}";
 ResultList<City> resultList = new ResultList<>();
        try {
            parseJsonStringToResultListGeneric(resultList,jsonResultList,new City());

            System.out.println(resultList.getResult().get(0).getFlows().get(0).getInprice());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 範型解析為list的
     * @param result
     * @param readString
     * @param <T>
     */
    public static <T> void parseJsonStringToResultListGeneric(ResultList<T> result, String readString, T t) throws Exception{

        ResultList<T> responseObject = JSON.parseObject(readString, new TypeReference<ResultList<T>>() {
        });
        if (responseObject.getResult() != null) {
            String valueString = JSON.toJSONString(responseObject.getResult());
            List<T> values = (List<T>) JSON.parseArray(valueString, t.getClass());

            //更新result
            result.setResult(values);
        }
        result.setReason(responseObject.getReason());
        result.setError_code(responseObject.getError_code());

    }


}