[英]How to access nested arrays in a JSON object (Android)
我的JSON的結構是
"posts":[
//Post 1.
{
"images":{
"small":{
"url": "http://..."
"width": 64
},
"large":{
"url": "http://..."
"width": 128
}
},
"caption":"..."
},
{
//Post 2
"images":{
"small":{
"url": "http://..."
"width": 64
},
"large":{
"url": "http://..."
"width": 128
}
},
"caption":"..."
}
]
我想從每個帖子中以字符串形式獲取帖子->圖片->大->網址 。
這是我所做的:
JSONArray jsonarray = jsonobject.getJSONArray("posts");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
JSONArray innerArray1 = jsonobject.getJSONArray("images");
JSONArray innerArray2 = innerArray1.getJSONArray(0); //gets "large"
JSONArray innerArray3 = innerArray2.getJSONArray(1); //gets "url"
String finalString = innerArray3.toString();
}
我期望finalString中的“ url”值,除了它最終為空。
我究竟做錯了什么?
“圖像”,“小”,“大”是JSONObjects (注意花括號),而不是JSONArrays(方括號),因此您需要這樣提取它們。
JSONArray jsonarray = jsonobject.getJSONArray("posts");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
JSONObject innerObject1 = jsonobject.getJSONObject("images");
JSONObject innerObject2 = innerObject1.getJSONObject("large"); //gets "large"
String finalString = innerObject2.getString("url");
}
只需使用翻新和GSON
構建一些稱為Posts.java
Post.java
Image.java
模型類
Posts.java
public class Posts {
@SerializedName("posts")
private List<Post> mPosts;
public List<Post> getPosts() {
return mPosts;
}
}
Post.java
public class Post {
@SerializedName("image")
private Image mImage;
public Image getImage() {
return mImage;
}
@SerializedName("caption")
public String mCaption;
public String getCaption() {
return mCaption;
}
}
圖像.java
public class Image {
@SerializedName("small")
private String mSmallImageUrl;
public Image getSmallImageUrl() {
return mSmallimageUrl;
}
@SerializedName("large")
public String mLargeImageUrl;
public String getLargeImageUrl() {
return mLargeImageUrl;
}
}
SerializedName
批注來自GSON
庫,該庫應該已經是您可以實現的Android Studio庫包的一部分。
實際上,它為您創建POJO
對象就是創建模型類。
使用翻新的實現很簡單,因為您可以這樣聲明一個RestAdapter
:
public class Client {
public static final String API_URL = ".......";
// Constructor which you can initialize somewhere else.
public Client() {
RestAdapter mAsyncRestAdapter = new RestAdapter.Builder()
.setEndpoint(API_URL)
.setClient(new OkClient(new OkHttpClient()))
.build();
}
// Implement Interfaces here somewhere.
}
然后像這樣為您的api端點創建接口,這有助於將事物解耦。 請注意, GET
注釋來自改型。 改造還允許POST
PUT
DELETE
等:
public interface IPosts {
@GET("/posts")
void getPosts(Callback<Posts> callback);
}
注意, Callback
是一種改進型回調,狀態為200 OK
,它將從API抓取Response
並將JSON
轉換為帶有GSON的Posts
對象。
您將像這樣實現接口:
public void getPosts(Callback<Posts> callback) {
IPosts posts = mAsyncRestAdapter.create(IPosts.class);
posts.getPosts(callback);
}
在您的應用中的某處,只需創建以下回調:
Callback<Posts> callback = new Callback<Posts>() {
@Override
public void onSuccess(Posts posts, Response response) {
// Do whatever you want with posts here
List<Post> posts = posts.getPosts(); // Get posts for example
}
@Override
public void onFailure(RetrofitError error) {
// Handle the error
}
};
// Pass this callback to the implementation of the interface to have it actually work
mClient.getPosts(callback);
這是您可以輕松訪問嵌套JSON對象的一種方式。 使用GSON進行改裝是真正的使用樂趣。 該方法的優點在於,您只需定義回調,接口和模型即可。 從中您可以看到代碼非常少且相互分離。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.