AAB
AAB

Reputation: 1

How to append HashMap one by one in JSONObject

My question is how to append HashMap to JsonObject on by one? Suppose I have HashMap called newspaperHashMap which is declared and Initilizedat the start of Program

HashMap<String, String> newspaperHashMap = new HashMap<>();
JSONObject j2 = new JSONObject();
 for(int tag_id =0 ; tag_id < 3; i++)
 {
    newspaperHashMap.put("s_date",newspaper_from_date.get(tag_id));
    newspaperHashMap.put("e_date",newspaper_to_date.get(tag_id));
    newspaperHashMap.put("papertype_id",newspaper_type_name.get(tag_id));
    j2 = new JSONObject(newspaperHashMap); ///
 }

But becase of last line j2 object is overwritten. Now I want to convert this HashMap to JSONObject. Any help will be appreciated. Thank You

Upvotes: 0

Views: 62

Answers (1)

Harsh Jatinder
Harsh Jatinder

Reputation: 873

You can directly put data inside jsonobject.

Try this :

JSONObject j2 = new JSONObject();
for(int tag_id =0 ; tag_id < 3; i++){
    j2.put("s_date",newspaper_from_date.get(tag_id));
    j2.put("e_date",newspaper_to_date.get(tag_id));
    j2.put("papertype_id",newspaper_type_name.get(tag_id));
}

Upvotes: 1

Related Questions