I want to convert HashMap to json array my code is as follow:
Map map = new HashMap();
map.put("first", "First Value");
map.put("second", "Second Value");
I have tried this but it didn't work
JSONArray mJSONArray = new JSONArray(Arrays.asList(map));
解决方案
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:
ok I'll show
JSONArray array=new JSONArray(obj.toString());