如何从 api 响应中获取单个 json 对象?

How to get individual json object from an api response?

实际上,我正在编写古兰经 API,其中 API 响应是

{
 "data": 
  {
    "surahs"[...],
    "edition":{...}
  }
}

在这个回复中,我只想要古兰经数组,看起来像

"surahs": [
        {
            "number": 1,
            "name": "سُورَةُ ٱلْفَاتِحَةِ",
            "englishName": "Al-Faatiha",
            "englishNameTranslation": "The Opening",
            "revelationType": "Meccan",
            "ayahs"[
               {
                    "number": 1,
                    "audio": "Some Url",
                    "text": "بِسْمِ ٱللَّهِ ٱلرَّحْمَٰنِ ٱلرَّحِيمِ",
                    "numberInSurah": 1,
                    "juz": 1,
                    "manzil": 1,
                    "page": 1,
                    "ruku": 1,
                    "hizbQuarter": 1,
                    "sajda": false
                },
                {...}
                {...}
            ]
        }
        {...}
        {...}
      ]

这就是我想要从这个回复中得到的,但我对如何获得它感到困惑, 我正在使用改造 2 来发出 HTTP 请求

这些是我的 POJO 类

public class Surah
{
    private int number;
    private String name;
    private String englishName;
    private String englishNameTranslation;
    private String revelationType;
    private List<Ayah> ayahs;
}

public class Ayah 
{
    private int number;
    private String audio;
    private String text;        //this field heave multiple languages
    private int numberInSurah;
    private int juz;
    private int manzil;
    private int page;
    private int ruku;
    private int hizbQuarter;
    private Boolean sajda;
}

这是 API 通话

public void MyApi(){
        WebApi mWebApi = WebApi.retrofit.create(WebApi.class);
        Call<Surah> call = mWebApi.getAllSurah();
        call.enqueue(new Callback<Surah>() {
            @Override
            public void onResponse(Call<Surah> call, Response<Surah> response) {
                if (response.isSuccessful()){

                }
            }
            @Override
            public void onFailure(Call<Surah> call, Throwable t) {
                Log.d(TAG, "onFailure:"+t.toString());
                Toast.makeText(MainActivity.this, "Response is OnFailure", Toast.LENGTH_SHORT).show();
            }
        });
    }

但是,我故意将 getAllSurah 类型列表更改为单个对象,因为那时我遇到了类似“Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $”的异常,因为它正在返回整个 json 对象作为响应。

您需要再添加一个 class 来保存您从 API

获得的全部响应
public class Data {

@SerializedName("surahs")
@Expose
private List<Surah> surahs = null;
@SerializedName("edition")
@Expose
private Edition edition;

public List<Surah> getSurahs() {
return surahs;
}

public void setSurahs(List<Surah> surahs) {
 this.surahs = surahs;
}

public Edition getEdition() {
 return edition;
}

public void setEdition(Edition edition) {
this.edition = edition;
}

}

然后在您的 api 调用中进行如下更改

public void MyApi(){
    WebApi mWebApi = WebApi.retrofit.create(WebApi.class);
    Call<Data> call = mWebApi.getAllSurah();
    call.enqueue(new Callback<Data>() {
        @Override
        public void onResponse(Call<Data> call, Response<Data> response) {
            if (response.isSuccessful()){
                response.getSurahs()  // this will return surah array to you
            }
        }
        @Override
        public void onFailure(Call<Data> call, Throwable t) {
            Log.d(TAG, "onFailure:"+t.toString());
            Toast.makeText(MainActivity.this, "Response is OnFailure", 
            Toast.LENGTH_SHORT).show();
        }
    });
}