简体   繁体   中英

Extracting specific section of JSON from URL in Android?

I am bulding a small app to track Bike Stations around the city, and I have an API that gives me the current status of the availability of bikes in bike stations from the company that provides the service.

My plan is to have a sort of interactive map, with all the markers for each of the bike stations, and when the user taps one of these, they get the information on that specific bike station. I have already all the locations coded in as markers on the map. What I need now is to be able to get the data for the specific bike station the user clicks.

An example of a part of the JSON I get from the API is below:

"number": INT,
"contract_name": "STRING",
"name": "STRING",
"address": "STRING",
"position": {
  "lat": DOUBLE,
  "lng": DOUBLE
},
"banking": BOOLEAN,
"bonus": BOOLEAN,
"bike_stands": INT,
"available_bike_stands": INT,
"available_bikes": INT,
"status": "STRING",
"last_update": 1588583133000
},
....

This structure is the same for all 100+ nodes of the JSON which I get from the API.

My question is, how would I go about filtering out one individual entry like such from the rest of the JSON. The parameter number is an ID unique to each bike station.

Is there a library that can do this for me? My idea (Very naive) was to save the whole JSON locally each time, and then go through it looking for "number":X and then parse out the data I needed, although this is obviously highly inefficient, I recognize that.

I am only interested in a part of each JSON, to be show to the user: the node's banking , bonus , available_bike_stands , available_bikes and status tags. The status tag is optional, it should simply tell me if the bike station is open (available) or closed.

Thank you very much,

Regards.

Get data from API --> Retrofit

Save local data--> SharePreference, Room

get a part of each JSON --> you create an object that contains some fields you need. when you use retrofit get data from API then it will return the result you desire

class YourClass {
    @SerializedName("number")
    var number: Int? = null
    @SerializedName("banking")
    var banking: Boolean? = null
    @SerializedName("bonus")
    var bonus: Boolean? = null
    @SerializedName("available_bike_stands")
    var availableBikeStands: Int? = null
    //... fields you need
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM