这是我的 JSON 字符串(动态变化):
这是整个 json 字符串中的一个 json 对象,它是一个数组。如果 JSON 对象的“ChildExists”值大于 1(例如下面的 4),则会出现一个名为“Categories”的数组并显示 4 个对象。如果任何这些对象的 childExist 值大于 1,就会出现一个名为“Categories”的 json 数组。
如果“childExists”的对象值大于 0,这将一遍又一遍地发生。我为此创建了 2 个具有以下属性的模型类:
public class Menu implements Serializable{
private static final long serialVersionUID = -3407147461924698222L;
String parentName;
String position;
String childExists;
String categoryName;
String categoryID;
String parentID;
List<Category> categories = new ArrayList<Category>();
//getters and setters
}
public class Category implements Serializable{
private static final long serialVersionUID = -3407147461924698222L;
String parentName;
String position;
String childExists;
String categoryName;
String categoryID;
String parentID;
//getters and setters
}
这就是我试图分解json的方式:
public static ArrayList<Menu> getMenuTreeBreakDown(JSONArray arg0) throws JSONException {
ArrayList<Menu> menuList = new ArrayList<Menu>();
for(int i = 0; i < arg0.length(); i++){
JSONObject jsonCategoryObject = arg0.getJSONObject(i);
Menu menu = new Menu();
String parentName = jsonCategoryObject.getString("ParentName");
String position = jsonCategoryObject.getString("Position");
String childExists = jsonCategoryObject.getString("ChildExists");
String categoryName = jsonCategoryObject.getString("Category_Name");
String categoryID = jsonCategoryObject.getString("Category_ID");
String parentID = jsonCategoryObject.getString("Parent_ID");
menu.setParentName(parentName);
menu.setPosition(position);
menu.setChildExists(childExists);
menu.setCategoryName(categoryName);
menu.setCategoryID(categoryID);
menu.setParentID(parentID);
if(!childExists.equalsIgnoreCase("0")){
//this part should happen over and over again if there is a category[]
JSONArray categoryArray = new JSONArray(jsonCategoryObject.getString("Categories"));
for (int x = 0; x < categoryArray.length(); x++){
JSONObject jsonProductObject1 = categoryArray.getJSONObject(x);
Category category = new Category();
category.setParentName(jsonProductObject1.getString("ParentName"));
category.setPosition(jsonProductObject1.getString("Position"));
category.setChildExists(jsonProductObject1.getString("ChildExists"));
category.setCategoryName(jsonProductObject1.getString("Category_Name"));
category.setCategoryID(jsonProductObject1.getString("Category_ID"));
category.setParentID(jsonProductObject1.getString("Parent_ID"));
menu.getCategories().add(category);
}
}
menuList.add(menu);
}
return menuList;
}
到目前为止,我无法得到正确的结果。任何帮助是极大的赞赏!
//得到这个工作:
使用了 GSON,更改了模型类:
public class Menu{
@SerializedName("ParentName")
String parentName;
@SerializedName("Position")
String position;
@SerializedName("ChildExists")
String childExists;
@SerializedName("Category_Name")
String categoryName;
@SerializedName("Category_ID")
String categoryID;
@SerializedName("Parent_ID")
String parentID;
@SerializedName("Categories")
List<Menu> categories = new ArrayList<Menu>();
//getters and setters
}
ArrayList<Menu> menuList = new ArrayList<Menu>();
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(arg0.toString()).getAsJsonArray();
for(JsonElement obj : jArray )
{
Menu menu = gson.fromJson( obj , Menu.class);
menuList.add(menu);
}