嵌套Json使用改造

时间:2016-11-14 14:27:08

标签: android json nested retrofit

我想阅读sub_category> id,来自json的名称使用改造

但是下面的代码会给出失败的回复。

  

java.lang.IllegalStateException:预期BEGIN_OBJECT但是   BEGIN_ARRAY在第2行第2列路径$

我的Json ....

[
{
    "id": "1",
    "name": "Invamore",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "102",
            "name": "3 sided Dangler"
        },
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "2",
    "name": "HUPS",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "104",
            "name": "Posters"
        },
        {
            "id": "105",
            "name": "Sunpack"
        }
    ]
},
{
    "id": "3",
    "name": "Xplore",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "106",
            "name": "Dangler"
        },
        {
            "id": "107",
            "name": "Vertical Streamer"
        }
    ]
},
{
    "id": "4",
    "name": "Xpress",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "103",
            "name": "Leaflet"
        },
        {
            "id": "108",
            "name": "Streamer"
        }
    ]
},
{
    "id": "5",
    "name": "Matrix",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "6",
    "name": "Instabrite",
    "sub_category": [
        {
            "id": "103",
            "name": "Leaflet"
        }
    ]
},
{
    "id": "7",
    "name": "Mileage",
    "sub_category": [
        {
            "id": "107",
            "name": "Vertical Streamer"
        }
    ]
},
{
    "id": "8",
    "name": "Onam Posm",
    "sub_category": [
        {
            "id": "101",
            "name": "Banner"
        },
        {
            "id": "106",
            "name": "Dangler"
        }
    ]
}]

Backend.java

public void pos_func(String user_id) {

    ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);      

    Call call = apiService.POS_MODEL_CALL(user_id);
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {

        }

        @Override
        public void onFailure(Call call, Throwable t) {
            Log.d("sk_log", "Failed! Error = " + t.getMessage());

        }
    });

}

ApiInterface.java

 @FormUrlEncoded
@POST("pos_distributed.php")
Call<ValuesPos> POS_MODEL_CALL(@Field("user_id") String user_id);

型号:来自pojoschema2pojo.com以上JSON

ValuesPos.java

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shkhan on 14/11/16.
 */
public class ValuesPos {
    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("sub_category")
    @Expose
    public List<SubCategory> subCategory = new ArrayList<SubCategory>();

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     * The subCategory
     */
    public List<SubCategory> getSubCategory() {
        return subCategory;
    }

    /**
     *
     * @param subCategory
     * The sub_category
     */
    public void setSubCategory(List<SubCategory> subCategory) {
        this.subCategory = subCategory;
    }

}

SubCategory.json

package com.example.shkhan.myapp.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * Created by shkhan on 14/11/16.
 */


public class SubCategory {

    @SerializedName("id")
    @Expose
    public String id;
    @SerializedName("name")
    @Expose
    public String name;

    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }

}

2 个答案:

答案 0 :(得分:1)

Json包含ValuesPos

的列表

ApiInterface.java调用单个ValuesPos

修改

更改ApiInterface.java以调用ValuesPos

列表
@FormUrlEncoded
@POST("pos_distributed.php")
Call<List<ValuesPos>> POS_MODEL_CALL(@Field("user_id") String user_id);

答案 1 :(得分:1)

以下是适合我的整个代码......(感谢您的帮助)

修改了两个类

ApiInterface.java

@FormUrlEncoded
@POST("pos_distributed.php")
Call<List<ValuesPos>> POS_MODEL_CALL(@Field("user_id") String user_id);

Backend.java

public void pos_func(String user_id) {
        dataArrayList1 = new ArrayList<>();
        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);


        Call <List<ValuesPos>> call = apiService.POS_MODEL_CALL(user_id);

        call.enqueue(new Callback<List<ValuesPos>>() {
            @Override
            public void onResponse(Call<List<ValuesPos>> call, Response<List<ValuesPos>> response) {
                Log.d("sk_log", "Status POS Code = successsss");
                dataArrayList1 = response.body();

                //ValuesPos valuesPos = (ValuesPos) response.body();
                Log.d("sk_log", "name==="+dataArrayList1.get(0).getName());
                Log.d("sk_log", "name==="+dataArrayList1.get(0).getSubCategory().get(0).getName());
                CustomerCollection.spinner_pos(dataArrayList1);


            }
            @Override
            public void onFailure(Call<List<ValuesPos>> call, Throwable t) {
                Log.d("sk_log", "Failed! Error = " + t.getMessage());               

            }
        });      
    }

:)