How to convert HashMap to json Array in android?

后端 未结 5 524
陌清茗
陌清茗 2020-12-03 03:20

I want to convert HashMap to json array my code is as follow:

Map map = new HashMap();

map.put(         


        
5条回答
  •  [愿得一人]
    2020-12-03 04:02

    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()+"]");
    

提交回复
热议问题