如何在对象键为数字时创建Java getter和setter [英] how to create Java getters and setters when object key is number

查看:164
本文介绍了如何在对象键为数字时创建Java getter和setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法创建Java Getters和Setters,因为我得到了Object Key的数字(数字)。
我会告诉你我的API响应。如何在不更改API的情况下实现此目的。

I cannot create Java Getters and Setters, because I got number(digit) for my Object Key. I will show you my API response. How can I achieve this without changing the API.

    {"api_status": true,
    "message": "",
     "data": {
        "0": {
          "id": "aaa",
          "name": "aaa",
          "address": "aaa",
          "category": "aaa",
          "open_24_hours": "aaa",
          "business_open": "",
          "business_close": "",
          "type": "0",
          "title": null,
          "latitude": "6.8729428",
          "longitude": "79.8689013",
          "city": "",
          "distance": "2.95555089735992"
           },
       "1": {
          "id": "bbb",
           "name": "bbb",
           "address": "bbb",
           "category": "bbb",
           "open_24_hours": "bbb",
           "business_open": "",
           "business_close": "",
           "type": "0",
           "title": null,
           "latitude": "6.8767581",
           "longitude": "79.8674747",
           "city": "",
           "distance": "2.915385898910569"
         },
   }
  }


推荐答案

使用以下类并使用您的json数据将其传递给GSON库和班级作为模范。你将获得你的模型,每个数据项都使用哈希表进行映射,其中key是你表示为字符串的数字。通过迭代哈希映射,你将获得keySet,这是你在json的数据键中的所有键。对于每个键,你可以得到itemData。

Use the below class and pass it to GSON library with your json data and the Class As a model . you will get your model, each data item is mapped with hashtable where key is your number which i represent as string By iterating over hash map you will get keySet which is your all keys in the data key of json. and for each key you can get itemData.

class JsonStructure{
public boolean api_status;
public String message
HashMap<String,ItemsData> data;
}


class ItemsData{
public String id;
public String name;
public String address;
public String category;
public String open_24_hours;
public String business_open;
public String business_close;
public String type;
public String title;
public String latitude;
public String longitude;
public String city;
public String distance;

}

用于改造Build

BuildRetrofit(){
 mOkHttpClient = new OkHttpClient.Builder()
                 .addInterceptor(interceptor)
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        mConverterFactory = GsonConverterFactory.create();
    String baseUrl = "http://dev.appslanka.com/";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(mOkHttpClient)
                .addConverterFactory(mConverterFactory)
                .build();
        mApi = retrofit.create(ApiInterface.class);
        }   

在ApiInterface中定义yoyr请求方法

interface ApiInterface{

  @GET("_test/placeInDistance/")
Call<JsonStructure> getResponseForApiCall();
}

现在将此方法称为改装呼叫结构:

Call<JsonStructure> call = mApi.getResponseForApiCall();
        Response<JsonStructure> response = call.execute();

解析此响应如下:

HashMap<String, ItemsData> map = response .data;
            Set<String> s = map.keySet();
            Iterator<String> i = s.iterator();
            while (i.hasNext()){
                String key = i.next();
                ItemsData data = map.get(key);
                String id = data.id;
                String name = data.name;
                String address = data.address;
                String category = data.category;
                String open24Hr = data.open_24_hours;
                String businessOpen = data.business_open;
                String close = data.business_close;
                String latitue = data.latitude;
                ..... etc

            }

这篇关于如何在对象键为数字时创建Java getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆