iT邦幫忙

0

android json 的問題

想要請問:
像下方這樣子的格式只能用在GeoJson嗎??? 不能用在json嗎???

{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "5945030094",
"name": "德興藥局",
"phone": "(03)8889408",
"address": "花蓮縣玉里鎮國武里中山路2段58號",
"mask_adult": 1269,
"mask_child": 190,
"updated": "2020/09/24 14:32:36",
"available": "星期一上午看診、星期二上午看診、星期三上午看診、星期四上午看診、星期五上午看診、星期六上午看診、星期日上午看診、星期一下午看診、星期二下午看診、星期三下午看診、星期四下午看診、星期五下午看診、星期六下午看診、星期日下午看診、星期一晚上看診、星期二晚上看診、星期三晚上看診、星期四晚上看診、星期五晚上看診、星期六晚上看診、星期日晚上看診",
"note": "口罩販售,營業時間,成人口罩200份,兒童口罩20份,售完為止。",
"custom_note": "",
"website": "",
"county": "花蓮縣",
"town": "玉里鎮",
"cunli": "國武里",
"service_periods": "NNNNNNNNNNNNNNNNNNNNN"
},
"geometry": {
"type": "Point",
"coordinates": [
121.315149,
23.333096
]
}
},

目前只想先截取裡面name 及經緯度的資料。
因這個格式跟政府open data的json 格式不太一樣,
例如: 政府的open data,
我可以用:

  jonObject = new JSONObject(s.toString());
                 JSONArray contacts = jonObject.getJSONObject("XML_Head").getJSONObject("Infos").getJSONArray("Info");

這樣子就可以讀到資料
但這樣子的格式,我有點想不通該如何寫?
我是用android studio 的java,
能否請再指點。

謝謝

2 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2021-01-24 11:45:26
最佳解答

參考這篇

謝謝!!我研究一下!!!

0
飛飛
iT邦新手 5 級 ‧ 2021-01-25 16:29:11

如果妳用Gson去轉就可以輕鬆解析

String json = response.body().string();
Gson gson = new Gson();
final Pharmacy pharmacies = gson.fromJson(json, Pharmacy.class);

Pharmacy


public class Pharmacy {
    private String type;
    private List<Features> features;


    public String getType() {
        return type;
    }

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

    public List<Features> getFeatures() {
        return features;
    }

    public void setFeatures(List<Features> features) {
        this.features = features;
    }
}

Features

class Features {
    private String type;
    private Property properties;
    private Geometry geometry;

    public String getType() {
        return type;
    }

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

    public Property getProperties() {
        return properties;
    }

    public void setProperties(Property properties) {
        this.properties = properties;
    }

    public Geometry getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }


    public Features(Property properties, Geometry geometry) {
        this.properties = properties;
        this.geometry = geometry;
    }
}

Property

public class Property {
    private String id;
    private String name;
    private String phone;
    private String address;
    private String mask_adult;
    private String mask_child;
    private String updated;
    private String available;
    private String note;
    private String custom_note;
    private String website;
    private String county;
    private String town;
    private String cunli;
    private String service_periods;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMask_adult() {
        return mask_adult;
    }

    public void setMask_adult(String mask_adult) {
        this.mask_adult = mask_adult;
    }

    public String getMask_child() {
        return mask_child;
    }

    public void setMask_child(String mask_child) {
        this.mask_child = mask_child;
    }

    public String getUpdated() {
        return updated;
    }

    public void setUpdated(String updated) {
        this.updated = updated;
    }

    public String getAvailable() {
        return available;
    }

    public void setAvailable(String available) {
        this.available = available;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getCustom_note() {
        return custom_note;
    }

    public void setCustom_note(String custom_note) {
        this.custom_note = custom_note;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getCounty() {
        return county;
    }

    public void setCounty(String county) {
        this.county = county;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

    public String getCunli() {
        return cunli;
    }

    public void setCunli(String cunli) {
        this.cunli = cunli;
    }

    public String getService_periods() {
        return service_periods;
    }

    public void setService_periods(String service_periods) {
        this.service_periods = service_periods;
    }

    public Property(String name, String phone, String address, String mask_adult, String mask_child) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.mask_adult = mask_adult;
        this.mask_child = mask_child;
    }
}

我要發表回答

立即登入回答