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;
}
这是我的对象的价值
{date=29-07-2017,Reciept=RC1, Amount=11800}
我在那里评论过,我的问题是 如何在Object中获取键值对。
我试过了
很多方式使用map.get(Object)
,但我没有做到。我只是
想知道如何获得date,Receipt,Amount
答案 0 :(得分:2)
试试这个。
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();
}
注意强>
JSON数据是非法的,因此我们必须将其转换。
String responseConvert = response.replace("=", ":");
使用Map<String,String>
和Iterator<String>
Map<String, String> map = new HashMap<>();
Iterator<String> sIterator = new JSONObject(responseConvert).keys();
获取密钥和值
// get key
String key = sIterator.next();
// get value
String value = new JSONObject(responseConvert).getString(key);