json数据解析(gson)

来源:互联网 发布:刘意 java视频 编辑:程序博客网 时间:2024/06/05 20:09
String json = "{" +                "    \"LTERSRP>=-65\": \"0\"," +                "    \"-80<=LTERSRP<-65\": \"10.4020\"," +                "    \"-85<=LTERSRP<-80\": \"0.6780\"," +                "    \"-90<=LTERSRP<-85\": \"46.7830\"," +                "    \"-95<=LTERSRP<-90\": \"0.3280\"," +                "    \"-100<=LTERSRP<-95\": \"3.4080\"," +                "    \"-105<=LTERSRP<-100\": \"13.9060\"," +                "    \"-110<=LTERSRP<-105\": \"1.7450\"," +                "    \"-115<=LTERSRP<-110\": \"0.0140\"," +                "    \"LTERSRP<-115\": \"24.7470\"" +                "}";        Type type = new TypeToken<Map<String,String>>(){}.getType();        @SuppressWarnings("unchecked")        Map<String,String> data = (Map<String,String>)new Gson().fromJson(json, type);        Iterator<Map.Entry<String, String>> iterator = data.entrySet().iterator();        while (iterator.hasNext()){            Map.Entry<String, String> next = iterator.next();            Log.e(TAG, "key : " + next.getKey() );            Log.e(TAG, "value : " + next.getValue() );        }

其中,json字符串是请求服务器之后返回的字符串,由于请求返回的json数据中的key和value都是需要的数据,所以采用gson才进行解析,转化成Map<String,String>,最后遍历Map即可得到所要的数据。

解析成功的数据后台打印的数据如下: