我正在尝试使用 Android 上的 Retrofit 解析以下 JSON 结构。
{
"payload": [
{
"name": "Rice",
"brands": [
{
"name": "Dawat",
"subProducts": [
{
"id": 1,
"name": "Basmati Long Grain",
"creditDays": 20,
"currency": "$",
"willDeliver": false,
"minPrice": 250,
"maxPrice": 400,
"sku": "1Kg",
"uom": ""
}
]
}
]
}
],
"messages": []
}
我使用http://www.jsonschema2pojo.org/制作了模型。我特别关注的键是有效负载-->名称、品牌-->名称和子产品-->名称。以下是我到目前为止所尝试过的。有人可以帮忙吗?我无法使用改造来解析这个 JSON 结构
productDetails.enqueue(new Callback<GetProductDetailsByPhoneNumber>() {
@Override
public void onResponse(Call<GetProductDetailsByPhoneNumber> call, Response<GetProductDetailsByPhoneNumber> response) {
Toast.makeText(getApplicationContext(), "Response mil gaya", Toast.LENGTH_LONG);
List<Payload> subProducts = new ArrayList<Payload>(response.body().payload);
}
@Override
public void onFailure(Call<GetProductDetailsByPhoneNumber> call, Throwable t) {
}
});
接口:
@GET("wholesaler/getProductDetailsByPhoneNumber")
Call<GetProductDetailsByPhoneNumber> getProducts(@Query("phoneNumber") String number);
getDService()
public API getDService(){
/**
* The Retrofit class generates an implementation of the API interface.
*/
if(service == null){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
service = retrofit.create(API.class);
}
return service;
}
有效负载.java
public class Payload {
public String name;
public List<Brand> brands;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Brand> getBrands() {
return brands;
}
public void setBrands(List<Brand> brands) {
this.brands = brands;
}
}
尝试使用此功能,因为您没有提供“有效负载”对象
productDetails.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if(response.body()!=null{
JsonObject jsonObject=response.body();
if(response.code() == 200){
if(jsonObject.has("payload"){
JsonArray dataArray = jsonObject.getAsJsonArray(HAS_DATA);
if (dataArray.size() > 0) {
Toast.makeText(getApplicationContext(), "Response Called", Toast.LENGTH_LONG);
//your further code
}
}
}
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
//logic for if response fails
}
});
在您的onResponse中使用此代码:
if(response.code()==HttpsURLConnection.HTTP_OK) { //HttpOk is the response code for 200
if (response.body() != null) {
if (response.body().payload!= null) {
//data is there in an array of type payload
//save all the data there
//like you want the name, you can get that by response.body().payload.name and you will get "rice"
//similarly if you want the name which is in subproducts array use : response.body().payload.get(0).brands.get(0).subProducts.get(0).name and you
// will get "Basmati Long Grain"
}
}
}
该代码将帮助您反序列化来自 JSON 的所有数据,您可以将其存储在任何您想要的地方。另外,我建议您也检查其他响应代码(例如 400 表示错误请求,500 表示内部服务器错误等)。看这里,你的 payload 在一个数组中,并且其中只有一个元素,这就是我使用
payload.get(0)
的原因。如果数组中有多个元素,您需要使用循环然后获取值,对于您的 brands 和 subProduct 数组也是如此。
您正在尝试获取对象 PayLoad,并且您有
{
"payload": [
{
"name"
这意味着它不以父级开头,因此您需要将答案保存在列表中,例如列表和迭代中的字母,您可以使用 Response.get(position) <-- and this one going to be your payload number position, I hope this can help you