【问题标题】:Unable to Loop through dynamic json string recursively in android无法在android中递归循环遍历动态json字符串
【发布时间】:2014-02-24 04:20:41
【问题描述】:

这是我的 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);
    }

【问题讨论】:

    标签: android json recursion arrays jsonobject


    【解决方案1】:

    您是否调试并检查了它到底在哪里中断?

    在可以的地方使用 for 循环代替 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/

    对于命名差异(根据webservice中的变量),可以使用如下注解 @序列化名称。 (所以不需要使用 Serializable)

    【讨论】:

    • 感谢您的学习!并更新答案,这对您的实施很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    相关资源
    最近更新 更多