问题
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);
}
回答1:
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)
来源:https://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android