使用Gson解析JSON数据

使用Gson解析JSON数据,第1张

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

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

Rules

JSON可以有以下格式

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

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

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

NOTE :左侧为JSON字符串,右侧为解析结构,方便查看。

福利 :截图是我在一个在线 JSON Editor 上截的,体验一下-->JSON Editor,很好用推荐给大家。

认清了JSON,就要解析它。

你可以使用的JSON库

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

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

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

步骤1:目标:将从webservice传回的json

1

{

"status": 0,

"result": {

"location": {

"lng": 103.98964143811,

"lat": 30.586643130352

},

"formatted_address": "四川省成都市双流县北一街154",

"business": "簇桥,金花桥",

"addressComponent": {

"city": "成都市",

"district": "双流县",

"province": "四川省",

"street": "北一街",

"street_number": "154"

},

"cityCode": 75

}

}

2

先普及下json数据格式定义: json数据只有两种格式.

一种是对象: 一个大括号包裹的内容就是一个对象.里面是无数个逗号相间隔的键值对

{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }

一种是数组:一个方括号包裹的内容就是一个数组,里面是无数个逗号相间隔的json对象

如:

{

"people": [

{

"firstName": "Brett",

"lastName": "McLaughlin",

"email": "aaaa"

},

{

"firstName": "Jason",

"lastName": "Hunter",

"email": "bbbb"

},

{

"firstName": "Elliotte",

"lastName": "Harold",

"email": "cccc"

}

]

}

END

步骤2 定义json数据格式对应的javaBean

public class Result {

private Integer status

private ResultDetail result

public Result() {

}

public Result(Integer status, ResultDetail result) {

super()

this.status = status

this.result = result

}

public ResultDetail getResult() {

return this.result

}

public Integer getStatus() {

return this.status

}

public void setResult( ResultDetail result ) {

this.result = result

}

public void setStatus( Integer status ) {

this.status = status

}

@Override

public String toString() {

return "Result [status=" + this.status + ", result=" + this.result

+ "]"

}

}

public class ResultDetail {

Location location

String formatted_address

AddressComponent addressComponent

String business

String cityCode

public ResultDetail() {

super()

// TODO Auto-generated constructor stub

}

public ResultDetail(Location location, String formatted_address,

AddressComponent addressComponent, String business, String cityCode) {

super()

this.location = location

this.formatted_address = formatted_address

this.addressComponent = addressComponent

this.business = business

this.cityCode = cityCode

}

public AddressComponent getAddressComponent() {

return this.addressComponent

}

public String getBusiness() {

return this.business

}

public String getCityCode() {

return this.cityCode

}

public String getFormatted_address() {

return this.formatted_address

}

public Location getLocation() {

return this.location

}

public void setAddressComponent( AddressComponent addressComponent ) {

this.addressComponent = addressComponent

}

public void setBusiness( String business ) {

this.business = business

}

public void setCityCode( String cityCode ) {

this.cityCode = cityCode

}

public void setFormatted_address( String formatted_address ) {

this.formatted_address = formatted_address

}

public void setLocation( Location location ) {

this.location = location

}

}

public class Location {

String lng

String lat

public Location() {

}

public Location(String lng, String lat) {

this.lng = lng

this.lat = lat

}

public String getLat() {

return this.lat

}

public String getLng() {

return this.lng

}

public void setLat( String lat ) {

this.lat = lat

}

public void setLng( String lng ) {

this.lng = lng

}

@Override

public String toString() {

return "Location [lng=" + this.lng + ", lat=" + this.lat + "]"

}

}

public class AddressComponent {

String city

String district

String province

String street

String street_number

public AddressComponent() {

super()

// TODO Auto-generated constructor stub

}

public AddressComponent(String city, String district, String province,

String street, String street_number) {

super()

this.city = city

this.district = district

this.province = province

this.street = street

this.street_number = street_number

}

public String getCity() {

return this.city

}

public String getDistrict() {

return this.district

}

public String getProvince() {

return this.province

}

public String getStreet() {

return this.street

}

public String getStreet_number() {

return this.street_number

}

public void setCity( String city ) {

this.city = city

}

public void setDistrict( String district ) {

this.district = district

}

public void setProvince( String province ) {

this.province = province

}

public void setStreet( String street ) {

this.street = street

}

public void setStreet_number( String street_number ) {

this.street_number = street_number

}

@Override

public String toString() {

return "AddressComponent [city=" + this.city + ", district="

+ this.district + ", province=" + this.province + ", street="

+ this.street + ", street_number=" + this.street_number + "]"

}

}

测试:

jsonString ( 目标json数据,已经在最上面写好的)

System.out.println( "jsonString:" + jsonString )

Gson gson = new Gson()

Result fromJson = gson.fromJson( jsonString.toString() ,

Result.class )

System.out.println( "******************************************" )

System.out.println( fromJson )

结果:

jsonString:{"status":0,"result":{"location":{"lng":103.98964143811,"lat":30.586643130352},"formatted_address":"四川省成都市双流县北一街154","business":"簇桥,金花桥","addressComponent":{"city":"成都市","district":"双流县","province":"四川省","street":"北一街","street_number":"154"},"cityCode":75}}

*******************************************

Result [status=0, result=ResultDetail [location=Location [lng=103.98964143811, lat=30.586643130352], formatted_address=四川省成都市双流县北一街154, addressComponent=AddressComponent [city=成都市, district=双流县, province=四川省, street=北一街, street_number=154], business=簇桥,金花桥, cityCode=75]]

可见,jsonString已经成功的被转换成了对应的javaBean


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8747698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-20
下一篇 2023-04-20

发表评论

登录后才能评论

评论列表(0条)

保存