当前位置: 首页 > 知识库问答 >
问题:

如何使用改型获得JSON响应

江航
2023-03-14
{
"records": [
    {
        "id": "744",
        "categoryname": "Floor Tiles",
        "photo": "",
        "width": "0",
        "height": "0",
        "secondlevelcategory": [
            {
                "id": "833",
                "categoryname": "Digital Vitrified Tiles",
                "photo": "http://image.com",
                "width": "1031",
                "height": "700"
            }
        ]
    },
    {
        "id": "744",
        "categoryname": "Floor Tiles",
        "photo": "",
        "width": "0",
        "height": "0",
        "secondlevelcategory": [
            {
                "id": "833",
                "categoryname": "Digital Vitrified Tiles",
                "photo": "http://image.com",
                "width": "1031",
                "height": "700"
            }
        ]
    }
] }

如何将此Json响应转换为改装bean我得到了Gson错误,就像使用JsonReader一样。setLenient(true)在第1行第1列路径接受格式错误的JSON$

private void callCategoryApi() {
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(GithubUserAPI.CATEGORY_API)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    GithubUserAPI githubUserAPI = retrofit.create(GithubUserAPI.class);
    Call<List<CatResponseData>> call = githubUserAPI.getCategory(Utils.key);
    call.enqueue(this);

}

@Override
public void onResponse(Call<List<CatResponseData>> call, Response<List<CatResponseData>> response) {
    int code = response.code();
    if (code == 200) {
        List<CatResponseData> res = response.body();
        for(CatResponseData resDat : res){
            System.out.println("=== Response : " + resDat.getCategoryname());
        }
    }else{
        Toast.makeText(this, "Did not work: " + String.valueOf(code), Toast.LENGTH_LONG).show();
    }
}

@Override
public void onFailure(Call<List<CatResponseData>> call, Throwable t) {
    System.out.println("=== Error : " + t.getMessage());
}

api调用

@POST("/apikey/{apikey}")
Call<List<CatResponseData>> getCategory(@Path("apikey") String user);

字符串CATEGORY\u API=“”https://api.callingservice.com";

请帮助我解决这个问题,如何将Json响应转换为Bean,我的Bean类如下

public class CategoryBean {
List<CatResponseData> responseData = new ArrayList<CatResponseData>(); } class CatResponseData {
String id;
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getCategoryname() {
    return categoryname;
}
public void setCategoryname(String categoryname) {
    this.categoryname = categoryname;
}
String categoryname; }

共有3个答案

程旭尧
2023-03-14

要转换,Gson需要在类字段上使用相同的名称。我建议使用该站点,您可以粘贴JSON,他以Gson格式返回类

对于您拥有的每个List,您需要一个包含对象的类。比如:

主要类别:

 import java.util.ArrayList;
    import java.util.List;
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    @Generated("org.jsonschema2pojo")
    public class Example {

    @SerializedName("records")
   @Expose
   private List<Record> records = new ArrayList<Record>();

    /**
    * No args constructor for use in serialization
* 
*/
public Example() {
}

/**
* 
* @param records
*/
public Example(List<Record> records) {
this.records = records;
}

/**
* 
* @return
* The records
*/
public List<Record> getRecords() {
return records;
}

/**
* 
* @param records
* The records
*/
public void setRecords(List<Record> records) {
this.records = records;
}

public Example withRecords(List<Record> records) {
this.records = records;
return this;
}

}

记录Java语言

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Record {

@SerializedName("id")
@Expose
private String id;
@SerializedName("categoryname")
@Expose
private String categoryname;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("width")
@Expose
private String width;
@SerializedName("height")
@Expose
private String height;
@SerializedName("secondlevelcategory")
@Expose
private List<Secondlevelcategory> secondlevelcategory = new ArrayList<Secondlevelcategory>();

/**
* No args constructor for use in serialization
* 
*/
public Record() {
}

/**
* 
* @param id
* @param secondlevelcategory
* @param height
* @param width
* @param categoryname
* @param photo
*/
public Record(String id, String categoryname, String photo, String width, String height, List<Secondlevelcategory> secondlevelcategory) {
this.id = id;
this.categoryname = categoryname;
this.photo = photo;
this.width = width;
this.height = height;
this.secondlevelcategory = secondlevelcategory;
}

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

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

public Record withId(String id) {
this.id = id;
return this;
}

/**
* 
* @return
* The categoryname
*/
public String getCategoryname() {
return categoryname;
}

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

public Record withCategoryname(String categoryname) {
this.categoryname = categoryname;
return this;
}

/**
* 
* @return
* The photo
*/
public String getPhoto() {
return photo;
}

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

public Record withPhoto(String photo) {
this.photo = photo;
return this;
}

/**
* 
* @return
* The width
*/
public String getWidth() {
return width;
}

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

public Record withWidth(String width) {
this.width = width;
return this;
}

/**
* 
* @return
* The height
*/
public String getHeight() {
return height;
}

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

public Record withHeight(String height) {
this.height = height;
return this;
}

/**
* 
* @return
* The secondlevelcategory
*/
public List<Secondlevelcategory> getSecondlevelcategory() {
return secondlevelcategory;
}

/**
* 
* @param secondlevelcategory
* The secondlevelcategory
*/
public void setSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) {
this.secondlevelcategory = secondlevelcategory;
}

public Record withSecondlevelcategory(List<Secondlevelcategory> secondlevelcategory) {
this.secondlevelcategory = secondlevelcategory;
return this;
}

}

Secondlevelcategory。Java语言

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Secondlevelcategory {

@SerializedName("id")
@Expose
private String id;
@SerializedName("categoryname")
@Expose
private String categoryname;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("width")
@Expose
private String width;
@SerializedName("height")
@Expose
private String height;

/**
* No args constructor for use in serialization
* 
*/
public Secondlevelcategory() {
}

/**
* 
* @param id
* @param height
* @param width
* @param categoryname
* @param photo
*/
public Secondlevelcategory(String id, String categoryname, String photo, String width, String height) {
this.id = id;
this.categoryname = categoryname;
this.photo = photo;
this.width = width;
this.height = height;
}

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

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

public Secondlevelcategory withId(String id) {
this.id = id;
return this;
}

/**
* 
* @return
* The categoryname
*/
public String getCategoryname() {
return categoryname;
}

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

public Secondlevelcategory withCategoryname(String categoryname) {
this.categoryname = categoryname;
return this;
}

/**
* 
* @return
* The photo
*/
public String getPhoto() {
return photo;
}

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

public Secondlevelcategory withPhoto(String photo) {
this.photo = photo;
return this;
}

/**
* 
* @return
* The width
*/
public String getWidth() {
return width;
}

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

public Secondlevelcategory withWidth(String width) {
this.width = width;
return this;
}

/**
* 
* @return
* The height
*/
public String getHeight() {
return height;
}

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

public Secondlevelcategory withHeight(String height) {
this.height = height;
return this;
}

}
巢宏富
2023-03-14

对不起,我不能发表评论,但我想帮忙;改造不可能只等待没有键名的项目数组吗?我的意思是,也许你需要告诉Json在迭代它之前获取对象“记录”

在我的改装中,我接收json数组,如:[{}、{}、{}、{}]

改为:{"记录": [{},{},{}]}

皇甫鸿远
2023-03-14

这应该是您给Retrofit的类。不是bean列表,因为它们封装在一个对象中。

 public class CategoryResponse {
     @SerializedName("records")
     List<CategoryBean> responseData;

     public List<CatResponseData> getResponseData() {
          return responseData;
     }
 }
 类似资料:
  • 我的JSON: 上面的代码是什么我有treid,所以可以任何一个请帮助我获得整个响应不仅是父类别。

  • 问题内容: 我有一个特定的Web服务,期望将JSON作为发布内容,并将向后吐出XML。我正在对所有网络通话使用Retrofit。这是我使用XML转换器设置Retrofit适配器的方式: 如您所见,我没有使用Gson转换器。我如何设法发布任何JSON?谢谢! 问题答案: 创建自定义。这将使用不同的转换器进行序列化和反序列化。 用法:

  • 我用Rocket编写了一个简单的web服务,每当数据作为200响应出现时,它都包含一个字符串向量。当出现错误时,我希望有自定义错误。我想对这些反应施加的结构应该如下所示https://stackoverflow.com/a/23708903/4402306,具体而言: 对于成功的响应: 对于错误: 对于错误,我混淆了在火箭中使用与实现我自己的错误结构,如枚举(使用火箭派生): 那么我应该用什么来实

  • 我想通过改型从get api获得响应,但我不知道如何检查,因此在给定完整代码和api响应的下面显示空响应, 我的回应是: 我的接口类: POJO: 下面是MainActivity类中的调用方法: 上面给出了完整的代码,但它显示空响应,因为我不知道如何检查和改装中的数据数组对象。 所以请谁能解释一下如何在改装中检查它。 谢谢。

  • 如何获得原始json输出。如果可能的话,我不想实现用户数据类和解析器。有什么办法吗? 标记重复的帖子(获得原始HTTP响应与改造)不是为Kotlin和我需要Kotlin版本。

  • 我试图从Web读取JSON数据,但该代码返回空结果。我不确定我做错了什么。