Parse JSON with two side values

I use volley to respond from the API, but the response consists of a pair of STATE_ID: STATE_NAME (ie value:value pair) , and I need both values ​​on different lines. I need these values ​​to fit in the spinner so that when the user selects State , I can also get its corresponding identifier.

 // JSON response { "1": "West bengal", "3": "Himachal Pradesh", "4": "Maharashtra", "11": "Queensland" } 

My code

 public class MainActivity extends AppCompatActivity { private static final String STATE = "MY_API"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void login(View v){ loginRequest(); } private void loginRequest() { StringRequest stringRequest = new StringRequest(Request.Method.POST, STATE, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("country_id","2"); return params; } }; RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); requestQueue.add(stringRequest); } } 
+5
source share
5 answers

You already have the iterate() method, as we discussed in the comments.

Prepare some work to give you the meaning:

 try { JSONObject jsonObject = new JSONObject(response); for (String key : iterate(jsonObject.keys())) { Toast.makeText(this, "Key : "+key+" Value: "+jsonObject.optString(key), Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } 

Please refer to iterate from this answer . I posted this as a new answer because the OP was not able to do this for the values. !!

+2
source

I used an iterator to find the key. Let this help:

  private void parseRespone(String response){ try { JSONObject MainjsonObject = new JSONObject(response) Iterator<String> iter= MainjsonObject.keys(); //To get keys of an object while (iter.hasNext()) { String key = (String)iter.next(); //Object value = jsonobj.get(key); //To use by object String valueStr = jsonobj.get.getString(key); Log.i("Jsonparsing", "key= "+key + "\n Value=" +valueStr ); Toast.makeText(getActivity(),"key= "+ key + "\n value= " + valueStr ,Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } } 

I could find an unknown key. Please register in your android studio ... here I also installed Toast ..

And call this function here ...

 ........... @Override public void onResponse(String response) { parseRespone(response); //Function to parse json } 

Thanks..

+2
source

You can use jsonObject.names() (or keys() for Iterator) to retrieve all the keys. After that, you can iterate through the array using the keys and store your lines.

https://developer.android.com/reference/org/json/JSONObject.html#names () https://developer.android.com/reference/org/json/JSONObject.html#keys ()

+1
source

It will be much better if you want to change your answer to something like this:

 [ {"ID":"1","name": "West bengal"}, {"ID":"3","name": "Himachal Pradesh"}, {"ID":"4","name": "Maharashtra"}, {"ID":"11","name": "Queensland"} ] 
+1
source

I tried this solution, and it worked out. Here, the "key" will fry key_value [1,3,4,11] and the value will print the names [West Bengal, Himachal Pradesh, Maharashtra, Queensland].

JSONObject list_object = inner_json_object.getJSONObject ("");

  for (String key : iterate(list_object.keys())) { // here key will be containing your OBJECT NAME YOU CAN SET IT IN TEXTVIEW. Toast.makeText(Activity.this, ""+key, Toast.LENGTH_SHORT).show(); String value = bank_list_object.optString(key); }` private <T> Iterable<T> iterate(final Iterator<T> i){ return new Iterable<T>() { @Override public Iterator<T> iterator() { return i; } }; } 
0
source

All Articles