Is it possible to loop through an array of data objects? All objects have the same structure inside. That is, use one class for parsing. Instead of creating a class for each object.
2 answers
If I understand correctly, this answer is suitable for you. Link
You need to use an iterator
Iterator<String> iter = json.keys(); while (iter.hasNext()) { String key = iter.next(); try { Object value = json.get(key); } catch (JSONException e) { // Something went wrong! } } The value will contain your object, which you can convert to your model using JsonParser. By the example of this code
String mJsonString = "..."; JsonParser parser = new JsonParser(); JsonElement mJson = parser.parse(mJsonString); MyDataObject object = gson.fromJson(mJson, MyDataObject.class); - Thank you very much, everything works) And the code is clean - Vitaly Robinovsky
|
Alternative method. You can not create a class at all. Well, if you need to create, you can simply put in class variables instead of an array
//News_list - is ArrayList String output = null; output = your Json string; JSONObject jsonResponse = new JSONObject(output);//json string JSONArray movies = jsonResponse.getJSONArray(your json header);//в вашем случае data for (int i = 0; i < movies.length(); i++) { JSONObject actor = null; actor = movies.getJSONObject(i); //получили объект и теперь можем делать что хотим String ID = null; ID = actor.getString("ID");//you json value tag News_list.add(new ArrayList<String>()); News_list.get(PageViewActivity.News_list.size() - 1).add(ID); } |
