Reputation: 361
I have this Json:
{
"withDrawAccountNumber": "1.10.100.1",
"Amount": "1000",
"creditor": {
"2.20.200.2": "1700",
"2.20.200.1": "300"
}
}
i want to get the creditor's key value in HashMap, output must be like this:
"2.20.200.2": "1700",
"2.20.200.1": "300"
i dont have any idea how i must do this.
Upvotes: 0
Views: 113
Reputation: 433
why not use this :
Map<String,String> testMap = new HashMap<String, String>();
String testJson = "{\r\n"
+ " \"withDrawAccountNumber\": \"1.10.100.1\",\r\n"
+ " \"Amount\": \"1000\",\r\n"
+ " \"creditor\": {\r\n"
+ " \"2.20.200.2\": \"1700\",\r\n"
+ " \"2.20.200.1\": \"300\"\r\n"
+ " }\r\n"
+ "}";
JSONObject ob = new JSONObject(testJson);
JSONObject cr = ob.getJSONObject("creditor");
Set<String> keys = cr.keySet();
for(String key : keys) {
testMap.put(key, cr.getString(key));
}
testMap.forEach((K,V)->System.out.println("key : "+K+" Value : "+V));
Upvotes: 1
Reputation: 1
I dont really see the purpose on why you would do that, but you could do something like this i guess:
JSONObject jsonObj = new JSONObject(obj);
HashMap<String,String> map = new HashMap<String,String>();
String value = jsonObj.getString("2.20.2001");
map.put("2.20.2001", value);
// ... use map here in what you want to achieve
Upvotes: 0