[英]How can Retrofit 2.0 parse nested JSON object?
我們的團隊決定使用Retrofit 2.0 ,我正在對該庫進行一些初步研究。 如標題所述,我想在我們的 Android 應用程序中通過 Retrofit 2.0 解析一些嵌套的 JSON 對象。
例如, 這是一個嵌套的 JSON 對象,其格式為:
{
"title": "Recent Uploads tagged android",
"link": "https://www.flickr.com/photos/tags/android/",
"description": "",
"modified": "2015-10-05T05:30:01Z",
"generator": "https://www.flickr.com/",
"items": [
{
"title": ...
"link": ...
"media": {"m":"This is the value I want to get:)"}
"description": ...
"published": ...
"author": ...
"author_id": ...
"tags": ...
},
{...},
...
]
}
我對items
數組中的 JSON 對象感興趣。 我注意到有一些關於通過 Retrofit 1.X 解析嵌套 JSON 對象的帖子,但是最新的 Retrofit 2.0 API 發生了很大變化,這在將它們適應新的 API 時令人困惑。
我想到了兩種可能的解決方案:
StringConverter
。 (我可能錯了~) 更新:我們實際上可以通過將JSONElement
設置為 HTTP API 接口的 pojo 並使用 Retrofit 提供的 GSONConverter 作為轉換器來獲取原始響應。
假設你完整的 JSON 看起來像
{
"title": "Recent Uploads tagged android",
"link": "https://www.flickr.com/photos/tags/android/",
"description": "",
"modified": "2015-10-05T05:30:01Z",
"generator": "https://www.flickr.com/",
"items": [
{
"member1": "memeber value",
"member2": "member value"
},
{
"member1": "memeber value",
"member2": "member value"
}
]
}
所以 Pojo 類將是
public class MainPojo {
private String title;
private String description;
private String link;
private String generator;
private String modified;
private ArrayList<Items> items;
// Getters setters
}
public class Items {
private String member2;
private String member1;
// Getters setters
}
注意:這是您 JSON 的類似解決方案。 如果 JSON 有其他鍵,則可以更改 Items.java 的成員。
將 Pojo 更新為新的 JSON
public class Items {
private String tags;
private String author;
private String title;
private String description;
private String link;
private String author_id;
private String published;
private Media media;
// Getters and Setters
}
public class Media {
private String m;
// Getters and Setters
}
以下代碼將有助於獲取嵌套的 json 對象和數組
例如:json
{
"similar_product":[
{ .....
}
],
"options":{
"Blouse Length":[
{ "value_id":"696556",
}
首先我們需要創建模型類,模型類項名稱在 json 項中相同,我們可以使用@SerializedName("for exact json name")
public class Product {
public Options options;
public void setOptions(Options options) {
this.options = options;
}
public Options getOptions() {
return options;
}
// length...
public class Options
{
@SerializedName("Blouse Length")
private ArrayList<BlouseLength> blouseLengths;
public void setBlouseLengths(ArrayList<BlouseLength> blouseLengths) {
this.blouseLengths = blouseLengths;
}
public ArrayList<BlouseLength> getBlouseLengths() {
return blouseLengths;
}
}
public class BlouseLength {
String value_id;
public void setValue_id(String value_id) {
this.value_id = value_id;
}
public String getValue_id() {
return value_id;
}
}
}
創建用於改造的接口以在 url 中獲取 json 項
// don't need to put values of id in retrofit
ex:: "/api-mobile_.php?method=getProductById&pid="
只需在查詢中傳遞 url 參數,它就會自動獲取 url
例如:
public interface Retrofit_Api {
@FormUrlEncoded
@GET("/api-mobile_.php?method=getProductById")
Call<Product> responseproduct(@Query("pid") String pid);
}
在你的主課上
String pid=editid.getText().toString();
final Retrofit adapter = new Retrofit.Builder()
.baseUrl(Product_url)
.addConverterFactory(GsonConverterFactory.create())
.build();
//Creating an object of our api interface
Retrofit_Api api = adapter.create(Retrofit_Api.class);
Call<Product> call = api.responseproduct(pid);
call.enqueue(new Callback<Product>() {
@Override
public void onResponse(Call<Product> call, Response<Product> response) {
ArrayList<Product.BlouseLength> p= new ArrayList(response.body().getOptions().getBlouseLengths());
Editadapter editadapter=new Editadapter(MainActivity.this,p);
recyclerView.setAdapter(editadapter);
}
@Override
public void onFailure(Call<Product> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
}
為您的模型使用 Gson 輕松解析https://github.com/google/gson
我的助手方法:
public String toJson(Object object) {
return gson.toJson(object);
}
public <T> T fromJson(String json, Class<T> classOfT) {
return gson.fromJson(json, classOfT);
}
public <T> T fromJson(JsonElement jsonElement, Class<T> classOfT) {
return gson.fromJson(jsonElement, classOfT);
}
你試過凌空抽射嗎? ...我更喜歡它現在是谷歌產品的改造。我有工作示例,如果你不介意我可以給你看。 http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
我忘了為內部 Class 對象添加@SerializedName
和@Expose
注釋,添加這些注釋后問題解決了。 像這樣:
JSON:
{"Id": 1,}
和班級成員:
@SerializedName("Id")
@Expose
private int id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.