如何使用改型访问jsonarray

时间:2018-10-31 05:18:37

标签: java android json retrofit2

如何使用districtID(key)访问name(key)值和retrofit值?

这是json文件...

{
    "error": false,
    "message": "District successfully fetched.",
    "districts": [
        {
            "districtID": "DIS260920181",
            "name": "Raipur"
        },
        {
            "districtID": "DIS260920182",
            "name": "Bilaspur"
        },
        {
            "districtID": "DIS011020186",
            "name": "korba"
        },
        {
            "districtID": "DIS011020187",
            "name": "jagdalpur"
        },
        {
            "districtID": "DIS021020188",
            "name": "surguja"
        },
        {
            "districtID": "DIS021020189",
            "name": "Mungeli"
        }
    ]
}

请帮助:(

3 个答案:

答案 0 :(得分:0)

将JSON数组转换为POJO类,然后使用改造的网络调用来调用api。

答案 1 :(得分:0)

使用此link

复制您的json,它为您提供了两个模型类,您应该将包含error,message和districts的父类作为您的请求输出。像这样

chmod -R

然后您将获得地区列表。您可以访问项目以及DistrictID和名称 希望对您有所帮助:)

答案 2 :(得分:0)

      for onResponse:
   --------------------------
        Call<ModalClass> daoCall = api.getconnectdata();
          modelclass.enqueue(new Callback<ModalClass>() {
           @Override
            public void onResponse(Call<ModalClass> call, Response<ModalClass> 
            response) {
                if(response.isSuccessful()){
                    modelclass= response.body();
             List<Districts>  list = modelclass.getAddData().getData();
             adapter = new CustomListAdapter(getApplicationContext(),list);
               listView.setAdapter(adapter);
                }
            }