Gson解析JSON數據中動態未知字段key的方法

轉載自:https://blog.csdn.net/jdsjlzx/article/details/76785239 有時在解析json數據中的字段key是動態可變的時候,因爲Gson是使用靜態註解的方式來設置實體對象的,所以咱們很難直接對返回的類型來判斷。但Gson在解析過程當中若是不知道解析的字段,就會將全部變量存儲在一個Map中,咱們只要實例化這個map就能動態地取出key和value了。 先給出一段jsondata,這是天氣預報的數據,其中day_20151002這種key是隨日期而變化的,在實體類中就不能當作靜態變量來處理,咱們就經過map來取出其映射對象。
java

{
    "resultcode": "200",
    "reason": "successed!",
    "result": {
        "sk": {
            "temp": "24",
            "wind_direction": "東北風",
            "wind_strength": "2級",
            "humidity": "28%",
            "time": "17:38"
        },
        "today": {
            "temperature": "15℃~26℃",
            "weather": "多雲轉晴",
            "wind": "東北風微風",
            "week": "星期日",
            "city": "桂林",
            "date_y": "2015年10月11日",
            "dressing_index": "溫馨",
            "dressing_advice": "建議着長袖T恤、襯衫加單褲等服裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。",
            "uv_index": "弱",
            "comfort_index": "",
            "wash_index": "較適宜",
            "travel_index": "較適宜",
            "exercise_index": "較適宜",
            "drying_index": ""
        },
        "future": {
            "day_20151011": {
                "temperature": "15℃~26℃",
                "weather": "多雲轉晴",
                "wind": "東北風微風",
                "week": "星期日",
                "date": "20151011"
            },
            "day_20151012": {
                "temperature": "16℃~27℃",
                "weather": "晴轉多雲",
                "wind": "微風",
                "week": "星期一",
                "date": "20151012"
            },
            "day_20151013": {
                "temperature": "16℃~26℃",
                "weather": "多雲轉晴",
                ,
                "wind": "微風",
                "week": "星期二",
                "date": "20151013"
            },
            "day_20151014": {
                "temperature": "17℃~27℃",
                "weather": "晴",
                "wind": "北風微風",
                "week": "星期三",
                "date": "20151014"
            },
            "day_20151015": {
                "temperature": "17℃~28℃",
                "weather": "晴",
                "wind": "北風微風",
                "week": "星期四",
                "date": "20151015"
            },
            "day_20151016": {
                "temperature": "17℃~30℃",
                "weather": "晴",
                "wind": "北風微風",
                "week": "星期五",
                "date": "20151016"
            },
            "day_20151017": {
                "temperature": "17℃~30℃",
                "weather": "晴",
                "wind": "北風微風",
                "week": "星期六",
                "date": "20151017"
            }
        }
    },
    "error_code": 0
}
複製代碼

相關的實體類以下:
json

public class FutureDay {
    private String temperature;
    private String weather;
    private String wind;
    private String week;
    private String date;
}

public class Result {
    private Sk sk;
    private Today today;
    private Map<String,FutureDay> future;
}
public class Sk {
    private String temp;
    private String wind_direction;
    private String wind_strength;
    private String humidity;
    private String time;
}
public class Today {
    private String temperature;
    private String weather;
    private String week;
    private String city;
    private String date_y;
    private String dressing_index;
    private String dressing_advice;
    private String uv_index;
    private String comfort_index;
    private String wash_index;
    private String travel_index;
    private String exercise_index;
    private String drying_index;
}
public class Response {
    private String resultcode;
    private String reason;
    private String error_code;
    private Result result;
}
複製代碼

具體解析過程以下代碼所示:
bash

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Map;
import weather.*;
import com.google.gson.Gson;


public class GsonParseDynamicKey {
    public static  void main( String args []){
        String jsondata = readJsonFile();//從文件中讀取出json字符串,並打印出來
        Gson gson = new Gson();
        System.out.println("Start Gson parse jsondata");   
        Response response = gson.fromJson(jsondata, Response.class);        
        System.out.println(response.toString());
        System.out.println(response.getResult().getSk().toString());
        System.out.println(response.getResult().getToday().toString());

        Map<String, FutureDay> future = response.getResult().getFuture(); //對動態的key,來建立map,間接從中取出實體類futrue。
        System.out.println("Keyset method");                     //這裏取出value的方法有兩種keySet() entrySet().都給出了遍歷的方法
        for (String key:future.keySet()){                        //遍歷取出key,再遍歷map取出value。
            System.out.println("key:"+key); 
            System.out.println(future.get(key).toString());
        }

        System.out.println("Entryset method");
        for (Map.Entry<String,FutureDay> pair:future.entrySet()){//遍歷取出鍵值對,調用getkey(),getvalue()取出key和value。
             System.out.println("key:"+pair.getKey());
             System.out.println(pair.getValue().toString());
       }    
}
複製代碼

這裏順便一提遍歷Map的兩種方法keySet(),entrySet()的差異。 keySet()方法返回的是key的集合set,entrySet()返回的是鍵值對的集合set。雖然二者從set遍歷取出元素的方法是同樣的,可是根據這個元素取出value的效率有些不一樣。前者取出的元素是key,還要去原map中遍歷取出value。 後者取出的元素是鍵值對,直接調用getkey(),getvalue()方法就能快速取出key和value。顯然在map中存在大量鍵值對時,使用entrySet()來取出value的效率更高。
ui

相關文章
相關標籤/搜索