2

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;
    }

2 Answers 2

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can still iterate:

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

3 Comments

for (Iterator key = jsonObjectchild.keys(); key.hasNext();) { JSONObject flatName = jsonObjectchild.getJSONObject(String.valueOf(key)); String name = flatName.getString("name"); String color = flatName.getString("color"); }
i try to do this but not wokring
@Tasnuvaoshin You're missing key.next() in String.valueOf()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.