【发布时间】:2018-02-15 21:05:53
【问题描述】:
我正在从 json 获取数据。在我的 json 中有 2 个数组。
- 主阵列
- 在主数组下有几个对象,在那个对象下我得到另一个数组。
问题是,当读取第一个对象时,它会转到 ParseLiveComment(GET CALL 参见下面的代码)并读取所有对象,然后返回所有注释并显示在列表中
但我想,当第一个对象读取时,它会转到 ParseLiveComment(GET CALL 参见下面的代码),然后添加第一个对象注释数据并添加到注释数组列表,依此类推
这是我的 Json
{
"status": 1,
"product": [
{
"id": 1,
"product_name_en": "testProduct",
"product_name_ar": "testProduct",
"product_description_en": "This is test product",
"product_description_ar": "This is test product",
"link": [
"http://kartpay.biz/api/v1/file/product/WMZv45E8UCWhIG95ammJSF9GePbz6CjgfskkRPK9.jpg"
],
"starting_price": "1000.000",
"sub_category_id": "1",
"status": "A",
"user_id": "1",
"view_count": null,
"like_count": null,
"comment_count": 1,
"comment": [
{
"product_id": "1",
"user_id": "1",
"comment": "Good and nice product"
}
],
"first_name_en": null,
"first_name_ar": null,
"last_name_en": null,
"last_name_ar": null,
"created_at": "Sep 4 2017 8:11 AM",
"updated_at": "Sep 4 2017 8:11 AM"
},
{
"id": 2,
"product_name_en": "testProducts",
"product_name_ar": "testProducts",
"product_description_en": "This is test products",
"product_description_ar": "This is test",
"link": [
"http://kartpay.biz/api/v1/file/product/FjtsAmhCxINBhleAl2sTtKcZehHokRaoO1T2aYW4.jpg",
"http://kartpay.biz/api/v1/file/product/LNG9gTlvnHzOpi3Hb5WNoWR8YN1oXuHwOOA6Nbqn.jpg"
],
"starting_price": "1000.000",
"sub_category_id": "1",
"status": "A",
"user_id": "1",
"view_count": null,
"like_count": null,
"comment_count": 0,
"comment": [],
"first_name_en": null,
"first_name_ar": null,
"last_name_en": null,
"last_name_ar": null,
"created_at": "Sep 4 2017 8:19 AM",
"updated_at": "Sep 4 2017 8:19 AM"
}
]
}
我已成功获取数组列表中的主数组,但我想将该评论数组存储在另一个数组列表中
这是我的两个数组列表的 Pojo 类
public class CardData implements ECCardData<Comment> {
private List<Comment> listItems;
@Override
public List<Comment> getListItems() {
return listItems;
}
public void setListItems(List<Comment> listItems) {
this.listItems = listItems;
}
}
评论 POJO
public class Comment {
private String commentText;
public String getCommentText() {
return commentText;
}
public void setCommentText(String commentText) {
this.commentText = commentText;
}
}
这是我的解析
public static ArrayList<CardData> PaeseLiveProducts(String response) throws JSONException {
ArrayList<CardData> alUser = new ArrayList<>();
JSONObject jsonRoot = new JSONObject(response);
JSONArray parentArray = jsonRoot.getJSONArray("product");
for (int j = 0; j < parentArray.length(); j++) {
JSONObject finalObject = parentArray.getJSONObject(j);
CardData user = new CardData();
int is= user.setId(finalObject.getInt("id"));
Comment comment = new Comment();
JSONArray jsonArray=finalObject.getJSONArray("link");
for (int i = 0; i < jsonArray.length(); i++) {
// Log.d("text", textArray.getString(i) );
user.setHeadBackgroundResource(jsonArray.getString(i));
}
JSONArray jsonArray1 = finalObject.getJSONArray("comment");
for (int k = 0; k < jsonArray1.length(); k++) {
// Log.d("text", textArray.getString(i) );
JSONObject jsonObject2 = jsonArray1.getJSONObject(k);
comment.setCommentText(jsonObject2.getString("comment"));
}
user.setListItems(PaeseLive(response));
alUser.add(user);
}
return alUser;
}
public static ArrayList<Comment> PaeseLiveComment(String response) throws JSONException {
ArrayList<Comment> alUser = new ArrayList<>();
JSONObject jsonRoot = new JSONObject(response);
JSONArray parentArray = jsonRoot.getJSONArray("product");
for (int j = 0; j < parentArray.length(); j++) {
JSONObject finalObject = parentArray.getJSONObject(j);
Comment comment = new Comment();
JSONArray jsonArray1 = finalObject.getJSONArray("comment");
for (int k = 0; k < jsonArray1.length(); k++) {
// Log.d("text", textArray.getString(i) );
JSONObject jsonObject2 = jsonArray1.getJSONObject(k);
comment.setCommentText(jsonObject2.getString("comment"));
}
alUser.add(comment);
}
return alUser;
// return alUser;
}
【问题讨论】:
-
你试过
GSON吗?我觉得Gson会比手动解析方便。 -
解析工作完美,但问题是当它读取第一个对象时,它会读取所有注释数组。我想在它读取第一个主要对象时读取该对象注释数组,第二个时读取第二个对象注释数组,依此类推
-
你的描述很混乱,你能解释一下当你得到第一个项目时,你到底想要什么。据我了解,您想获取 cmets 数组并对其进行解析?
-
是的,完全正确。但它应该在评论中添加 Arraylist
标签: android arrays json parsing arraylist