使用GSON解析List类型等的字符串

json格式的文件内容如下:

[{"code":"ICBC","name":"中国工商银行"},{"code":"ABC","name":"中国农业银行"},{"code":"CCB","name":"中国建设银行"},{"code":"CMB","name":"招商银行"},{"code":"CEB","name":"中国光大银行"},{"code":"CIB","name":"兴业银行"}]


可以看到这里外面是[]中括号,说明了这个是数组类似类型的(如List、Map等),里面有code和name可以组成对象的属性。

所以应该可以使用GSON进行解析,可以猜测其结构为List<BankInfo>,那么如何解析成这样子的类型呢?

代码如下:


@Override
    public void onClick(View v) {
        String content = getFromAssets(assetsBankResourceFileName);
        Type type = new TypeToken<ArrayList<BankInfo>>() {}.getType();
        List<BankInfo> bankInfoList = (List<BankInfo>) GsonUtils.fromJson(content, type);

        txt.setText(content);
    }

    public String getFromAssets(String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


package com.robot.tongbanjie.util;

import java.lang.reflect.Type;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonUtils {
    /**
     * @Title: toJson
     * @Description:
     * @param bean
     * @return String 返回类型
     * @throws:
     */
    public static String toJson(Object bean){
        Gson gson=new Gson();
        return gson.toJson(bean);
    }
    
    public static String toJson(Object bean,Type type){
        Gson gson=new Gson();
        return gson.toJson(bean, type);
    }
    
    /**
     * @Title: fromJson
     * @Description:
     * @param json
     * @param type
     * @return T 返回类型
     * @throws:
     */
    public static Object fromJson(String json,Type type){
        Gson gson=new Gson();
        return gson.fromJson(json, type);
    }
    
    /**
     * @Title: fromJson
     * @Description:
     * @param <T>
     * @param json
     * @param classOfT
     * @return T 返回类型
     * @throws:
     */
    public  static <T>T fromJson(String json,Class<T> classOfT){
        Gson gson=new Gson();
        return gson.fromJson(json, classOfT);
    }
}



若解析如下格式内容的文件:

{"1":{"code":"ICBC","name":"中国工商银行"},"2":{"code":"ABC","name":"中国农业银行"},"3":{"code":"CCB","name":"中国建设银行"},"4":{"code":"CMB","name":"招商银行"},"5":{"code":"CEB","name":"中国光大银行"},"6":{"code":"CIB","name":"兴业银行"}}

可以看到这里1、2、3等数字对应的是一个个的对象。



解析的代码如下:

@Override
    public void onClick(View v) {
        String content = getFromAssets(assetsBankResourceFileName);
        Type type = new TypeToken<Map<String, BankInfo>>(){}.getType();
        Map<String, BankInfo> bankInfoMap = (Map<String, BankInfo>) GsonUtils.fromJson(content, type);

        txt.setText(content);
    }

    public String getFromAssets(String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


你可能感兴趣的:(使用GSON解析List类型等的字符串)