Gson解析数据

一、Json简介

JSON官网(中文版):http://www.json.org/json-zh.html

JSON(JavaScript Object Notation)是一种轻量级(轻量级?简单、易操作、快捷)的数据交换格式。主要目的就是给出一套通用的数据格式,大家按照这种格式定义自己的数据,方便数据的交换。特点是(相对来说) 易于人阅读和编写,易于机器解析和生成 。

Rules

  • “名/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)

JSON可以有以下格式

1.对象是一个无序的“ ‘名称/值’ 对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

Gson解析数据_第1张图片
object的 名称 /值 结构

2.数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

Gson解析数据_第2张图片
“值的类型”可以是哪些数据类型

以上是最基本的json知识,想深入了解的,请移步官网。

下面举个栗子给大家尝尝:

{
    "type": "forecast1d",
    "weatherinfo": [
        {
            "city": "北京",
            "cityid": "1",
            "temp1": "22℃",
            "temp2": "10℃",
            "weather": "晴",
            "ptime": "11:00"
        },
        {
            "city": "上海",
            "cityid": "2",
            "temp1": "24℃",
            "temp2": "12℃",
            "weather": "晴",
            "ptime": "11:00"
        }
    ]
}

栗子好难看,上截图(截图太小看不清...戳这里看大图<-- !):

Gson解析数据_第3张图片
一个简单的JSON串

NOTE:左侧为JSON字符串,右侧为解析结构,方便查看。
福利:截图是我在一个在线JSON Editor上截的,体验一下-->JSON Editor,很好用推荐给大家。

二、使用Gson在Android中解析Json

认清了JSON,就要解析它。

你可以使用的JSON库

JSONObject(源自Android官方)、
Gson(源自Google)、
Jackson(第三方开源库)、
FastJSON(第三方开源库)、
等。。。

本篇文章使用Gson解析JSON,Gson地址:http://code.google.com/p/google-gson/


google-gson


无法下载?百度云分享一下http://pan.baidu.com/s/1kTur5xd,提取密码:5oae

根据JSON串的结构定义一个类(这里我们把这个类叫Result),我们直接把得到的JSON串解析成这个类。class Result定义如下:

public class Result {

    private String type;
    private List weatherinfo;

    public void setType(String type) {
        this.type = type;
    }

    public void setWeatherinfo(List weatherinfo) {
        this.weatherinfo = weatherinfo;
    }

    public String getType() {
        return type;
    }

    public List getWeatherinfo() {
        return weatherinfo;
    }

    public static class WeatherinfoBean {

        private String city;
        private String cityid;
        private String temp1;
        private String temp2;
        private String weather;
        private String ptime;

        public void setCity(String city) {
            this.city = city;
        }

        public void setCityid(String cityid) {
            this.cityid = cityid;
        }

        public void setTemp1(String temp1) {
            this.temp1 = temp1;
        }

        public void setTemp2(String temp2) {
            this.temp2 = temp2;
        }

        public void setWeather(String weather) {
            this.weather = weather;
        }

        public void setPtime(String ptime) {
            this.ptime = ptime;
        }

        public String getCity() {
            return city;
        }

        public String getCityid() {
            return cityid;
        }

        public String getTemp1() {
            return temp1;
        }

        public String getTemp2() {
            return temp2;
        }

        public String getWeather() {
            return weather;
        }

        public String getPtime() {
            return ptime;
        }
    }

}

定义好了待解析成的class之后,接下来使用Gson解析JSON串就可以了:

Gson gson = new Gson();
Result result = gson.fromJson(jsonData, Result.class);
//取出 weatherInfo
List weather =  result .getWeatherinfo();
String city = " ";
for(WeatherinfoBean city : weather ){
    city += city.getCity()+"\n";
}

So easy!有木有?

难点

  1. 如何定义这个待解析成的类?其实很简单,看到JSON结构里面有{ }你就定义一个类,看到[ ]你就定义一个List即可,最后只剩下最简单的如String、int等基本类型直接定义就好。
  2. 内部嵌套的类,请使用public static class className { }。
  3. 类内部的属性名,必须与JSON串里面的Key名称保持一致。这三点请自行对照我们上面举的Result的栗子,都有对应。

你可能感兴趣的:(Gson解析数据)