Hello to all. There is such:

{ "status": "ok", "meta": { "count": 1 }, "data": { "raz": { "dva": { "tri": { "results": 1234, "more1": 5678, "more2": "abc" ... } } } } } 

How to bring all this into a more comfortable look?

 ArrayList , HashMap 

Ie, I don’t understand what kind there should be a class with so many attachments, so that I can only extract from the whole result:

 private String results; ... public String getResults() { return results; } 

I apologize for such an incomprehensible explanation, I do not know how to present everything correctly yet.

Now it looks like this:

 public class Json extends AsyncTask<String, String, String> { private Context context; private String method; private String url; private String[] array; private String results; public Json (Context context, String method, String url, String[] array){ this.context = context; this.method = method; this.url = url; this.array = array; } @Override protected void onPreExecute() { super.onPreExecute(); } protected String doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); try { for (int i = 0; i < array.length; i++) { String[] s = array[i].split("="); params.add(new BasicNameValuePair(s[0], s[1])); } JSONObject json = jParser.makeHttpRequest(url, method, params); results = json.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(String string) { Toast.makeText(Main.this, results, Toast.LENGTH_LONG).show(); } } 

Where to change / add?

    1 answer 1

    You can simply throw this line into JSONObject and get the results by the necessary keys.

     String jsonString = "{ "status": "ok", "meta": { "count": 1 }, "data": { "raz": { "dva": { "tri": { "results": 1234, "more1": 5678, "more2": "abc" ... } } } } }"; JSONObject object = new JSONObject(jsonString); int results = object.getJSONObject("data").getJSONObject("raz").getJSONObject("dva").getJSONObject("tri").getInt("results"); 
    • Everything turned out so simple) It helped! But you can still example using ArrayList, HashMap? To allow all attachments to complain to the adapter, and then I extracted the right one. Ie from the last one, which is "tri": {..} - Sergey
    • Although there is already an analogy. All clear! - Sergey
    • Yes, you can deliver the value from the JSONObject by key and use the same key in HashMap - Kirill Stoianov
    • @ Sergey, you are trying to turn the data tree into a flat structure for nothing. Nothing good can happen. For example, will the keys start repeating at different levels? - Eugene Krivenja
    • @EugeneKrivenja do not quite understand what it is about, but the keys will not be exactly repeated. I unfortunately are just starting to learn json and if you have any thoughts on this, I will only be happy - Sergey