0

I have this line of code and I would like to drain numbers from the "list". Which is one good way to implement that? I am new to developing and this is a place I've been stuck for days.

    protected Void doInBackground(Void... voids) {
        try {
            URL url = new URL("https://{myUrl}");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (line != null){
                line = bufferedReader.readLine();
                data = data + line; }

            JSONArray JA = new JSONArray(data);
            for (int i = 0; i <JA.length(); i++ ){
                JSONObject JO = (JSONObject) JA.get(i);
                singleParsed = "list:" + JO.get("list");
                dataParsed = dataParsed + singleParsed;
            } } catch (MalformedURLException e) {
            e.printStackTrace(); } catch (IOException e) {
            e.printStackTrace(); } catch (JSONException e) {
            e.printStackTrace(); }
        return null; }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        MainActivity.data.setText(this.dataParsed);
    } ```

JSON file

 {
   "common":{
      "mixed":"yes",
      "nums":{
         "list":[
            1,
            2,
            3,
            4
         ],
         "other":[
            5
         ]
      }}
} 

3 Answers3

0

try this pseudo code :

for(int i=0 ; i<JA.length() ; i++)
    {
        try
        {
            JSONObject jobj1 = (JSONObject) JA.get(i);
            JSONObject jobj2 = jobj1.getJSONObject("common");
            JSONObject jobj3 = (JSONObject) jobj2.get("nums");
            JSONArray jarray_list = jobj3.getJSONArray("list");

            //now you have a json array in that there are item of 'list'

        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
Sina Soheili
  • 121
  • 1
  • 1
  • 11
0

If you use Retrofit to consume service, you could add Gson to serialize automagically the Json to a Dto Object.

Here is a example: Get nested JSON object with GSON using retrofit

FacuArg
  • 64
  • 4
0

If I assume your using GET method on your API without passport or token authentication you need to do it like this.

You need to define the API method Get the the JSON in inputstream and parse it.

            URL url = new URL("https://{myUrl}"); //use HttpsURLConnection if you are sure it is https
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.connect();

            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder result = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            Log.e("ResponseCode",""+conn.getResponseCode()); // 200 is success
            Log.e("TAG", "result" + result); // your json

            if (conn.getResponseCode() == 200) {
            try {

                JSONObject resultOBJ= new JSONObject(result.toString());
                String common = (String)resultOBJ.get("common");
                JSONObject commonOBJ= new JSONObject(common);
                String nums = (String)commonOBJ.get("nums");

                 //do the rest ...

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
L2_Paver
  • 596
  • 5
  • 11