How to convert HashMap to json Array in android?

Try this,

public JSONObject (Map copyFrom) 

Creates a new JSONObject by copying all name/value mappings from the given map.

Parameters
copyFrom a map whose keys are of type String and whose values are of supported types.

Throws
NullPointerException if any of the map’s keys are null.

Basic usage:

JSONObject obj=new JSONObject(yourmap);

get the json array from the JSONObject

Edit:

JSONArray array=new JSONArray(obj.toString());

Edit:(If found Exception then You can change as mention in comment by @krb686)

JSONArray array=new JSONArray("["+obj.toString()+"]");

Leave a Comment