这是我的 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);
}
最佳答案
您是否调试并检查过它到底在哪里损坏?
在可以的地方使用 for every 循环代替 for(;;)
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?
此外,如果您有复杂的 json,那么最好使用 Gson 来映射数据和模型类。例如:
Gson gson = new Gson();
ModelClass modelClass= new ModelClass();
modelClass= gson.fromJson(responseContent,ModelClass.class);
//where responseContent is your jsonString
Log.i("Web service response", ""+modelClass.toString());
https://code.google.com/p/google-gson/
对于命名差异(根据Web服务中的变量),可以使用注释,例如 @序列化名称。 (所以不需要使用Serialized)
关于android - 无法在android中递归地循环动态json字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480634/