通过动态JSON在Android的递归字符串无法循环递归、字符串、动态、Android

2023-09-11 20:13:11 作者:就是这个范儿

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

这是从整个JSON字符串这是一个阵列中的一个JSON对象。 如果JSON对象具有大于1的值为ChildExists(例如4象下面),阵列被称为关键字出现和4个对象将被显示。如果任何对象的childExist值变得大于1,JSON数组被称为分类将出现。

Android Studio中直接写入JSON格式字符串

这会发生一遍又一遍,如果为childExists对象的值大于0,我创建了2模型类为这个具有以下属性:

 公共类菜单实现Serializable {

    私有静态最后长的serialVersionUID = -3407147461924698222L;

    字符串parentName;
    字符串的位置;
    字符串childExists;
    字符串categoryName;
    字符串categoryId;
    字符串的parentID;
    名单<类别>类别=新的ArrayList<类别>();

       // getter和setter

}

公共类分类实现Serializable {

    私有静态最后长的serialVersionUID = -3407147461924698222L;

    字符串parentName;
    字符串的位置;
    字符串childExists;
    字符串categoryName;
    字符串categoryId;
    字符串的parentID;

        // getter和setter

}
 

这是我如何试图打破JSON:

 公共静态的ArrayList<菜单> getMenuTreeBreakDown(JSONArray为arg0)抛出JSONException {

        ArrayList的<菜单>菜单列表=新的ArrayList<菜单>();
        的for(int i = 0; I< arg0.length();我++){
            的JSONObject jsonCategoryObject = arg0.getJSONObject(ⅰ);
            菜单菜单=新菜单();

            串parentName = jsonCategoryObject.getString(ParentName);
            字符串的位置= jsonCategoryObject.getString(位置);
            串childExists = jsonCategoryObject.getString(ChildExists);
            字符串categoryName = jsonCategoryObject.getString(CATEGORY_NAME);
            字符串categoryId = jsonCategoryObject.getString(CATEGORY_ID);
            串的parentID = jsonCategoryObject.getString(PARENT_ID);

            menu.setParentName(parentName);
            menu.setPosition(位置);
            menu.setChildExists(childExists);
            menu.setCategoryName(类别名);
            menu.setCategoryID(的categoryID);
            menu.setParentID(的parentID);

            如果(!childExists.equalsIgnoreCase(0)){
                //这部分应该发生一遍又一遍,如果还有一类是[]
                JSONArray categoryArray =新JSONArray(jsonCategoryObject.getString(分类));

                为(中间体X = 0 X  - 其中; categoryArray.length(); X ++){

                    JSONObject的jsonProductObject1 = categoryArray.getJSONObject(X);
                    类别类别=新类别();

                    category.setParentName(jsonProductObject1.getString(ParentName));
                    category.setPosition(jsonProductObject1.getString(位置));
                    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()加(类);

                }
            }
            menuList.add(菜单);
        }
        返回菜单列表;
    }
 

到目前为止,我无法得到正确的结果。任何帮助是极大AP preciated!

//得到这个工作:

用于GSON,改变了模型类:

 公共类菜单{

    @SerializedName(ParentName)
    字符串parentName;
    @SerializedName(位置)
    字符串的位置;
    @SerializedName(ChildExists)
    字符串childExists;
    @SerializedName(CATEGORY_NAME)
    字符串categoryName;
    @SerializedName(CATEGORY_ID)
    字符串categoryId;
    @SerializedName(PARENT_ID)
    字符串的parentID;
    @SerializedName(分类)
    名单<菜单>类别=新的ArrayList<菜单>();
// getter和setter
}

ArrayList的<菜单>菜单列表=新的ArrayList<菜单>();
    GSON GSON =新GSON();
    JsonParser分析器=新JsonParser();
    。JsonArray jArray = parser.parse(arg0.toString())getAsJsonArray();

    对于(JsonElement OBJ:jArray)
    {
        菜单菜单= gson.fromJson(OBJ,Menu.class);
        menuList.add(菜单);
    }
 

解决方案

你调试和检查确切部位在哪里突破?

使用for each循环在那里你可以,而不是为(;;)

http://docs.oracle.com/ JavaSE的/ 1.5.0 /文档/引导/语言/ foreach.html

如何在Java for each循环的工作?

另外,如果你有一个复杂的JSON,您可以使用GSON地图数据和模型类会更好。例如:

  GSON GSON =新GSON();
    ModelClass modelClass =新ModelClass();
    modelClass = gson.fromJson(responseContent,ModelClass.class);
//其中,responseContent是你的jsonString
    Log.i(Web服务响应,+ modelClass.toString());
 

的https://$c$c.google.com/p/google -gson /

有关命名差异(根据在web服务中的变量),可以使用像注解 @SerializedName。 (所以没有必要使用序列化)

This is my JSON String (this changes dynamically):

This is one json object from the entire json string which is an array. If the JSON object has value greater than 1 for "ChildExists" (for example 4 like below), an array called "Categories" appears and 4 objects will be shown. If the childExist value of any of those objects become greater than 1, a json array called "Categories" will appear.

This will happen over and over again if the object's value for "childExists" is greater than 0. I created 2 model classes for this with the following attributes :

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

}

This is how I tried to break down the 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;
    }

So far I could not get the correct results. Any help is greatly appreciated!

//got this working:

Used GSON, changed the model class:

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);
    }

解决方案

Did you debug and check where exactly does it break?

Use a for each loop where you can instead of for(;;)

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

How does the Java for each loop work?

Also, If you have a complicated json, you may be better off using Gson to map data and your model classes. Eg.:

    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/

For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName. (So no need to use Serializable)