无法在android中递归地循环遍历动态json字符串 [英] Unable to Loop through dynamic json string recursively in android
问题描述
这是我的 JSON 字符串(动态变化):
This is my JSON String (this changes dynamically):
这是来自整个 json 字符串的一个 json 对象,它是一个数组.如果 JSON 对象的ChildExists"值大于 1(例如下面的 4),则会出现一个名为Categories"的数组,并会显示 4 个对象.如果这些对象中的任何一个的 childExist 值大于 1,则会出现一个名为Categories"的 json 数组.
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.
如果childExists"的对象值大于 0,这将一遍又一遍地发生.我为此创建了 2 个具有以下属性的模型类:
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
}
这就是我试图分解 json 的方式:
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!
//让这个工作:
使用了 GSON,改变了模型类:
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?
使用 for each 循环代替 for(;;)
Use a for each loop where you can instead of for(;;)
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
另外,如果你有一个复杂的 json,你最好使用 Gson 来映射数据和你的模型类.例如:
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/一个>
对于命名差异(根据webservice中的变量),可以使用注释,如@序列化名称.(所以不需要使用Serializable)
For Naming discrepancies(according to the variables in webservice), can use annotations like @SerializedName. (So no need to use Serializable)
这篇关于无法在android中递归地循环遍历动态json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!