Search code examples
androidarraysjsonparsingkey-value

JsonObject parsing without keys


Iam trying to parse a json content to my android app. Iam using firebase db to get the results.

What I have

private void StartParse() {
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                webserviceUrl, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d("Repsonse", response.toString());

            try {
                // Parsing json object response
                // response will be a json object

                String cover = response.optString("cover").toString();
                String ip_address = response.optString("ip_address").toString();
                String port = response.optString("port").toString();
                String info = response.optString("info").toString();
                String uid = response.optString("uid").toString();

                Map<String, String> map = new HashMap<String, String>();
                map.put(cover, cover.toString());
                // and so on..
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Response", "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hide the progress dialog
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);

}

What I need is that I have this json response here:

{  
"-KFOmfn3XcmM3BsrWYcZ":{  
  "company":"A17",
  "country":"France",
  "email":"[email protected]",
  "uid":"720363f7-84f1-4a35-9551-9422da991835"
},
"-KTCLolTzHO0KFcuQR3B":{  
  "company":"B13",
  "country":"Sweden",
  "email":"[email protected]",
  "uid":"7eade882-c182-441e-8793-94c6a1879a52"
},
"-KFcuQR3B1KTCLsa43BK10":{  
  "company":"C23",
  "country":"Denmark",
  "email":"France",
  "uid":"7eade882-c182-441e-8793-94c6a1879a52"
},
"-CLHOcuQoKFzRTl3BKx0T":{  
  "company":"4SS",
  "country":"Italy",
  "email":"[email protected]",
  "uid":"7eade882-c182-441e-8793-94c6a1879a52"
}
}

the response is json file but how can I just grab the values? so I can have an array with the values only.


Solution

  •     Iterator<String> iter = json.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        try {
            Object value = json.get(key);
        } catch (JSONException e) {
            // Something went wrong!
        }
    }