简体   繁体   中英

How can I get the key value pair from Map in android

public Map<String, Object> toMap(JSONObject fees_history) throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();
    jsonArray = new JSONArray();
    int j = 0;
    Iterator<String> keysItr = fees_history.keys();
    while (keysItr.hasNext()) {
        String key = keysItr.next().toString();
        Object value = fees_history.get(key);
        if (value instanceof JSONArray) {
            value = toList((JSONArray) value);
        } else if (value instanceof JSONObject) {
            value = toMap((JSONObject) value);
            //How can i get the String value from this Object here
            //here i am logging
            Log.e("Value",value.toString());
            Unpaid_fees_detail_adapter unpaid_fees_detail_adapter = new Unpaid_fees_detail_adapter(ReportActivity.this, unpaid_fees_details);
            listView.setAdapter(unpaid_fees_detail_adapter);
        }
        map.put(key, value);
    }
    return map;
}

Here is My value of Object which in am getting

{date=29-07-2017,Reciept=RC1, Amount=11800}

I have commented there and my question is how can I get the key value pair inside the Object.

I have tried a lot of way by using map.get(Object) but I am failed to do it.I just want how can I get the separate value of date,Receipt,Amount

Try this.

String response = "{date=29-07-2017,Reciept=RC1, Amount=11800}";
String responseConvert = response.replace("=", ":");
Map<String, String> map = new HashMap<>();
Iterator<String> sIterator = null;
try {
    sIterator = new JSONObject(responseConvert).keys();
    while (sIterator.hasNext()) {
        // get key
        String key = sIterator.next();
        // get value
        String value = new JSONObject(responseConvert).getString(key);
        map.put(key, value);
        Log.e("TAG", "key: " + key + ",value:" + value);
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Note

  1. JSON data is illegal,so we must convert it.

     String responseConvert = response.replace("=", ":"); 
  2. Use Map<String,String> and Iterator<String>

     Map<String, String> map = new HashMap<>(); Iterator<String> sIterator = new JSONObject(responseConvert).keys(); 
  3. Get key and value

     // get key String key = sIterator.next(); // get value String value = new JSONObject(responseConvert).getString(key); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM