-2

我有一个小问题gson,让我解释一下。

我无法提取这个距离和单位:

"distance_info": {
    "distance": 101.3,
    "unit": "km"

我的 GSON 序列化程序类

import java.util.List;
import com.google.gson.annotations.SerializedName;

public class SearchResponseShow {

@SerializedName("status")
public String status;

@SerializedName("lang")
public String lang;

@SerializedName("guid")
public String guid;

@SerializedName("name")
public String name;

@SerializedName("tags")
public String tags;

@SerializedName("address")
public String address;

@SerializedName("description")
public String description;

@SerializedName("size")
public String size;

@SerializedName("url")
public String url;

@SerializedName("email")
public String email;

@SerializedName("fax")
public String fax;

@SerializedName("tel")
public String tel;

@SerializedName("total_votes")
public String total_votes;

@SerializedName("total_values")
public String total_values;

@SerializedName("rate")
public String rate;

@SerializedName("open2424")
public String open2424;

@SerializedName("category_main_name")
public String category_main_name;

@SerializedName("category_name")
public String category_name;

@SerializedName("category_main_name2")
public String category_main_name2;

@SerializedName("category_name2")
public String category_name2;

@SerializedName("category_main_name3")
public String category_main_name3;

@SerializedName("category_name3")
public String category_name3;

@SerializedName("park_type")
public String park_type;

@SerializedName("park_handicap")
public String park_handicap;

@SerializedName("park_free")
public String park_free;

@SerializedName("park_description")
public String park_description;

@SerializedName("datemodinfo")
public String datemodinfo;

@SerializedName("sponsor")
public String sponsor;

@SerializedName("sponsorstart")
public String sponsorstart;

@SerializedName("sponsorend")
public String sponsorend;

@SerializedName("town")
public String town;

@SerializedName("area")
public String area;

@SerializedName("latitude")
public String latitude;

@SerializedName("longitude")
public String longitude;

@SerializedName("distance_info")
public Object distance_info;

@SerializedName("zip")
public String zip;

@SerializedName("image")
public String image;

@SerializedName("open")
public int open;

public List<openinghours> openinghours;

@SerializedName("query")
public String query;
}

我的 JSON :

"status": "",
"lang": "",
"guid": "",
"name": "",
"tags": "",
"address": "",
"description": "",
"size": "",
"url": "",
"email": "",
"fax": "",
"tel": "",
"total_votes": "",
"total_value": "",
"rate": ,
"open2424": "",
"category_main_name": "",
"category_name": "",
"category_main_name2": "",
"category_name2": "",
"category_main_name3": "",
"category_name3": "",
"park_type": "",
"park_handicap": "",
"park_free": "",
"park_description": "",
"datemodinfo": "",
"sponsor": "",
"sponsorstart": "",
"sponsorend": "",
"zip": "",
"town": "",
"area": "",
"latitude": "",
"longitude": "",
"distance_info": {
    "distance": 101.3,
    "unit": "km"
},
    "image": "",
"open": "",
"openinghours": [{
    "schedules": [{
        "periods": [{
            "": "",
            "": ""
        },
        {
            "": "",
            "": ""
        }],
        "": ""
    },
    {
        "": [{
            "": "",
            "": ""
        },
        {
            "": "",
            "": ""
        }],
        "": ""
    },
    {
        "": [{
            "": "",
            "": ""
        },
        {
            "": "",
            "": ""
        }],
        "": ""
    },
    {
        "": [{
            "": "",
            "": ""
        },
        {
            "": "",
            "": ""
        }],
        "": ""
    },
    {
        "": [{
            "": "",
            "": ""
        },
        {
            "": "",
            "": ""
        }],
        "": ""
    },
    {
        "": ""
    },
    {
        "": ""
    }],
    "": ""
}]

}

对不起,我的英语不好。

4

1 回答 1

0

在 GSON 序列化程序类中:

import java.util.HashMap;    
import java.util.Map;

@SerializedName("distance_info")
public Map<String, String> distance_info = new HashMap<String, String>();

访问字符串:

response.distance_info.get("distance")
response.distance_info.get("unit")
于 2013-07-25T13:26:29.350 回答