我很难知道解析JSON的方法,但当我尝试访问我想要的值时,Android Studio会给我以下消息:
正如你所看到的那样,关键字“大”没有价值,但显然它是,无论如何,这是我的json解析方法
public static List<News> parseJSONtoNews(JSONArray jsonArray) throws JSONException {
List<News> newsList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
News news = new News();
JSONObject jsonObjectNews = jsonArray.getJSONObject(i);
JSONArray jsonArrayCategories = jsonObjectNews.getJSONArray(JSONKeys.KEY_CATEGORIES);
int category = getCatgories(jsonArrayCategories);
news.setCategories(category);
news.setiD(jsonObjectNews.getInt("id"));
JSONObject jsonObjectTitle = jsonObjectNews.getJSONObject("title");
news.setTitle(jsonObjectTitle.getString("rendered"));
JSONObject jsonObjectContent = jsonObjectNews.getJSONObject("content");
news.setContent(jsonObjectContent.getString("rendered"));
JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");
JSONObject jsonObjectSizes = jsonObjectMediaDetails.getJSONObject("sizes");
JSONObject jsonObjectMediumLarge = jsonObjectSizes.getJSONObject("large");
news.setImageURL(jsonObjectMediumLarge.getString("source_url"));
newsList.add(news);
}
return newsList;
}
public static int getCatgories(JSONArray jsonArray) throws JSONException{
int categories = 0;
for (int i = 0; i<jsonArray.length(); i++){
categories = jsonArray.getInt(i);
}
return categories;
}
这是我想解析的JSON
[
{
"id": 742,
"title": {
"rendered": “title”
},
"content": {
"rendered": “content”
},
"categories": [
4
],
"tags": [],
"better_featured_image": {
"file": "2016/05/20160520_191324.jpg",
"sizes": {
"thumbnail": {
"file": "20160520_191324-150x150.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-150x150.jpg"
},
"medium": {
"file": "20160520_191324-300x169.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-300x169.jpg"
},
"medium_large": {
"file": "20160520_191324-768x432.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-768x432.jpg"
},
"large": {
"file": "20160520_191324-1024x576.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324-1024x576.jpg"
}
},
"post": 742,
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/05/20160520_191324.jpg"
},
},
{
"id": 745,
"title": {
"rendered": “title”
},
"content": {
"rendered": “content”
},
"categories": [
4
],
"tags": [],
"better_featured_image": {
"file": "2016/05/20160520_191324.jpg",
"sizes": {
"thumbnail": {
"file": "20160520_191324-150x150.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-150x150.jpg"
},
"medium": {
"file": "20160520_191324-300x169.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-300x169.jpg"
},
"medium_large": {
"file": "20160520_191324-768x432.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-768x432.jpg"
},
"large": {
"file": "20160520_191324-1024x576.jpg",
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324-1024x576.jpg"
}
},
"post": 742,
"source_url": "http://www.aarc.com.mx/wp-content/uploads/2016/06/20160520_191324.jpg"
},
}
]
正如你所看到的那样存在关键的“大”但我无法访问它,它可以使用“缩略图”键,所以我不知道如何处理这个,我希望你们能帮助我! 感谢
答案 0 :(得分:2)
它在我看来:
JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_media");
JSONObject jsonObjectMediaDetails = jsonObjectImage.getJSONObject("media_details");
jsonObjectMediaDetails
不应该存在。我在JSON中的任何地方都看不到media_details
。我也没有看到better_featured_media
。我认为它应该是better_featured_image
。
尝试:
JSONObject jsonObjectImage = jsonObjectNews.getJSONObject("better_featured_image");
JSONObject jsonObjectSizes = jsonObjectImage.getJSONObject("sizes");