android - 无法在android中递归地循环动态json字符串

标签 android recursion arrays json

这是我的 JSON 字符串(动态更改):

enter image description here

这是整个 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/

相关文章:

recursion - 结构感应的终止

javascript - 数组的数组到输出中的对象列表(没有额外的数组)

javascript - 如何编写内置原型(prototype)的替代品?

android - Android 上的移动网站宽度问题

python - 获取每个节点的最大树深度

android - 架构组件: Observer keep observing even after removing it on onDestroy

java - 如何在递归中将字符串输出添加到数组

arrays - 删除偶数/奇数以在以二进制表示的数字数组中查找缺失的数字

android - 在android中创建自己的操作栏

java - 如果条件导致程序强制关闭