从URL在Android中提取特定的JSON部分?

huangapple 未分类评论48阅读模式
英文:

Extracting specific section of JSON from URL in Android?

问题

我正在构建一个小应用程序来跟踪城市周围的自行车站,我有一个API可以从提供该服务的公司那里获取自行车站的当前可用情况。

我的计划是创建一个交互式地图,显示每个自行车站的所有标记。当用户点击其中一个标记时,他们会获得有关该特定自行车站的信息。我已经将所有位置编码为地图上的标记。现在我需要能够获取用户点击的特定自行车站的数据。

下面是我从API获取的JSON的一部分示例:

{
  "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
},
...

这个结构对于我从API获取的所有100+个节点都是相同的。

我的问题是,我该如何从其他JSON中筛选出类似上述结构的单个条目。参数number是每个自行车站的唯一ID。

有没有一个库可以为我做到这一点?我的想法(非常幼稚)是每次将整个JSON保存在本地,然后通过查找"number":X来遍历它,然后解析出我需要的数据,尽管我认识到这显然效率很低。

我只对每个JSON的一部分感兴趣,以显示给用户:节点的bankingbonusavailable_bike_standsavailable_bikesstatus字段。状态标记是可选的,它应该简单地告诉我自行车站是否开放(可用)或关闭。

非常感谢,
祝好。

英文:

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.

答案1

得分: 0

从API获取数据 --> Retrofit

保存本地数据 --> SharePreference,Room

获取每个JSON的一部分 --> 您创建一个包含所需一些字段的对象。当您使用Retrofit从API获取数据时,它将返回您想要的结果。

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
    //... 需要的字段
}
英文:

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
}

huangapple
  • 本文由 发表于 2020年5月4日 17:38:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61589196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定