• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android JSON only OBJECT Fetching with Iteration

I have a json file which contains only objects now i cant able to fetch all the object with iteration as here is no array.

Here is the code : That i have tried in my android studio & JSon Data Screenshot enter image description here

  @Override
    protected String doInBackground(String... strings) {
        try {
            url = new URL("https://api.myjson.com/bins/r7dj6");
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.connect();
            inputStream = httpURLConnection.getInputStream();

           bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
           String line =" ";

           while ((line = bufferedReader.readLine()) != null){
               stringBuffer.append(line);

           }

           String fullfile = stringBuffer.toString();

            JSONObject jsonObject = new JSONObject(fullfile);
            JSONObject jsonObjectchild = jsonObject.getJSONObject("Apartment");




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


        return null;
    }

Here is a complete example. Works for me.

HttpURLConnection connection = null;
try {
    URL url = new URL("https://api.myjson.com/bins/r7dj6");
    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    InputStream inputStream = connection.getInputStream();

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    StringBuilder stringBuffer = new StringBuilder();
    while ((line = reader.readLine()) != null){
        stringBuffer.append(line);
    }

    JSONObject jsonObject = new JSONObject(stringBuffer.toString());
    JSONObject apartmentObject = jsonObject.getJSONObject("Apartment");
    Iterator<String> keys = apartmentObject.keys();
    while (keys.hasNext()) {
        String flatName = keys.next();
        JSONObject flat = apartmentObject.getJSONObject(flatName);
        String age = flat.getString("age");
        String color = flat.getString("color");
        String name = flat.getString("name");
        String owner = flat.getString("owner");
        String partner = flat.getString("partner");
        Log.d("Flat", flatName + ": " + age + ", " + color + ", " + name + ", " + owner + ", " + partner);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (connection != null) {
        connection.disconnect();
    }
}
  • 1
Reply Report

You can still iterate:

for (Iterator key=jsonObjectchild.keys();key.hasNext();) {
    JSONObject flatName = json.get(key.next());
    ...
}
  • 0
Reply Report