【问题标题】:get map inside map java android获取地图里面的地图java android
【发布时间】:2021-04-07 06:45:33
【问题描述】:

我有这个 JSON 响应。 我想获取 datetime 内的时间数据。

{
   "code":200,
   "status":"OK",
   "results":{
      "datetime":[
         {
            "times":{
               "Imsak":"04:21",
               "Sunrise":"-",
               "Fajr":"04:31",
               "Dhuhr":"11:41",
               "Asr":"14:58",
               "Sunset":"-",
               "Maghrib":"17:54",
               "Isha":"18:46",
               "Midnight":"-"
            },
            "date":{
               "timestamp":1617667200,
               "gregorian":"2021-04-06",
               "hijri":"1442-08-24"
            }
         }
      ],
      "location":{
         "latitude":-6.966667,
         "longitude":110.416664,
         "elevation":-9999.0,
         "country":"",
         "country_code":"ID",
         "timezone":"Asia/Jakarta",
         "local_offset":7.0
      },
      "settings":{
         "timeformat":"HH:mm",
         "school":"Ithna Ashari",
         "juristic":"Shafii",
         "highlat":"None",
         "fajr_angle":18.0,
         "isha_angle":17.0
      }
   }
}

如何获取对象“次数”并将其放入地图。 如何获取单个值以及数组。

【问题讨论】:

标签: java android json


【解决方案1】:

下面的代码将创建“times”对象。喜欢:

        String json = "{\"code\":200,\"status\":\"OK\",\"results\":{\"datetime\":[{\"times\":{\"Imsak\":\"04:21\",\"Sunrise\":\"-\",\"Fajr\":\"04:31\",\"Dhuhr\":\"11:41\",\"Asr\":\"14:58\",\"Sunset\":\"-\",\"Maghrib\":\"17:54\",\"Isha\":\"18:46\",\"Midnight\":\"-\"},\"date\":{\"timestamp\":1617667200,\"gregorian\":\"2021-04-06\",\"hijri\":\"1442-08-24\"}}],\"location\":{\"latitude\":-6.966667,\"longitude\":110.416664,\"elevation\":-9999.0,\"country\":\"\",\"country_code\":\"ID\",\"timezone\":\"Asia/Jakarta\",\"local_offset\":7.0},\"settings\":{\"timeformat\":\"HH:mm\",\"school\":\"Ithna Ashari\",\"juristic\":\"Shafii\",\"highlat\":\"None\",\"fajr_angle\":18.0,\"isha_angle\":17.0}}}";
        try {
            JSONObject rootJsonObject = new JSONObject(json);
            JSONObject resultJsonObject = rootJsonObject.getJSONObject("results");
            JSONArray dateTimeJsonArray = resultJsonObject.getJSONArray("datetime");
            JSONObject timeJsonObject = new JSONObject(dateTimeJsonArray.get(0).toString());
            JSONObject timeJson = new JSONObject(timeJsonObject.getString("times"));
            Log.d("TAG","Time json = "+timeJson);
        }catch (JSONException jsonException){
            Log.d("TAG","JSON Exception: "+jsonException.getLocalizedMessage());
        }

【讨论】:

    【解决方案2】:
    public class Example {
    
    @SerializedName("code")
    @Expose
    private Integer code;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("results")
    @Expose
    private Results results;
    
    public Integer getCode() {
    return code;
    }
    
    public void setCode(Integer code) {
    this.code = code;
    }
    
    public String getStatus() {
    return status;
    }
    
    public void setStatus(String status) {
    this.status = status;
    }
    
    public Results getResults() {
    return results;
    }
    
    public void setResults(Results results) {
    this.results = results;
    }
    
    }
    
    
    
    public class Results {
    
    @SerializedName("datetime")
    @Expose
    private List<Datetime> datetime = null;
    @SerializedName("location")
    @Expose
    private Location location;
    @SerializedName("settings")
    @Expose
    private Settings settings;
    
    public List<Datetime> getDatetime() {
    return datetime;
    }
    
    public void setDatetime(List<Datetime> datetime) {
    this.datetime = datetime;
    }
    
    public Location getLocation() {
    return location;
    }
    
    public void setLocation(Location location) {
    this.location = location;
    }
    
    public Settings getSettings() {
    return settings;
    }
    
    public void setSettings(Settings settings) {
    this.settings = settings;
    }
    
    }
    
    public class Datetime {
    
    @SerializedName("times")
    @Expose
    private HashMap<String,String> times;
    @SerializedName("date")
    @Expose
    private Date date;
    
    public HashMap<String,String> getTimes() {
    return times;
    }
    
    public void setTimes(HashMap<String,String> times) {
    this.times = times;
    }
    
    public Date getDate() {
    return date;
    }
    
    public void setDate(Date date) {
    this.date = date;
    }
    
    }
    
    
    public class Settings {
    
    @SerializedName("timeformat")
    @Expose
    private String timeformat;
    @SerializedName("school")
    @Expose
    private String school;
    @SerializedName("juristic")
    @Expose
    private String juristic;
    @SerializedName("highlat")
    @Expose
    private String highlat;
    @SerializedName("fajr_angle")
    @Expose
    private Double fajrAngle;
    @SerializedName("isha_angle")
    @Expose
    private Double ishaAngle;
    
    public String getTimeformat() {
    return timeformat;
    }
    
    public void setTimeformat(String timeformat) {
    this.timeformat = timeformat;
    }
    
    public String getSchool() {
    return school;
    }
    
    public void setSchool(String school) {
    this.school = school;
    }
    
    public String getJuristic() {
    return juristic;
    }
    
    public void setJuristic(String juristic) {
    this.juristic = juristic;
    }
    
    public String getHighlat() {
    return highlat;
    }
    
    public void setHighlat(String highlat) {
    this.highlat = highlat;
    }
    
    public Double getFajrAngle() {
    return fajrAngle;
    }
    
    public void setFajrAngle(Double fajrAngle) {
    this.fajrAngle = fajrAngle;
    }
    
    public Double getIshaAngle() {
    return ishaAngle;
    }
    
    public void setIshaAngle(Double ishaAngle) {
    this.ishaAngle = ishaAngle;
    }
    
    }
    
    public class Location {
    
    @SerializedName("latitude")
    @Expose
    private Double latitude;
    @SerializedName("longitude")
    @Expose
    private Double longitude;
    @SerializedName("elevation")
    @Expose
    private Double elevation;
    @SerializedName("country")
    @Expose
    private String country;
    @SerializedName("country_code")
    @Expose
    private String countryCode;
    @SerializedName("timezone")
    @Expose
    private String timezone;
    @SerializedName("local_offset")
    @Expose
    private Double localOffset;
    
    public Double getLatitude() {
    return latitude;
    }
    
    public void setLatitude(Double latitude) {
    this.latitude = latitude;
    }
    
    public Double getLongitude() {
    return longitude;
    }
    
    public void setLongitude(Double longitude) {
    this.longitude = longitude;
    }
    
    public Double getElevation() {
    return elevation;
    }
    
    public void setElevation(Double elevation) {
    this.elevation = elevation;
    }
    
    public String getCountry() {
    return country;
    }
    
    public void setCountry(String country) {
    this.country = country;
    }
    
    public String getCountryCode() {
    return countryCode;
    }
    
    public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
    }
    
    public String getTimezone() {
    return timezone;
    }
    
    public void setTimezone(String timezone) {
    this.timezone = timezone;
    }
    
    public Double getLocalOffset() {
    return localOffset;
    }
    
    public void setLocalOffset(Double localOffset) {
    this.localOffset = localOffset;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2012-12-03
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多