且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在android中将HashMap转换为json数组?

更新时间:2023-01-09 21:39:02

试试这个,

public JSONObject (Map copyFrom) 

通过复制给定映射中的所有名称/值映射来创建一个新的 JSONObject.

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

参数copyFrom 一个映射,其键是 String 类型,其值是受支持的类型.

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

投掷如果地图的任何键为空,则为 NullPointerException.

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

基本用法:

JSONObject obj=new JSONObject(yourmap);

从 JSONObject 中获取 json 数组

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

(如果发现异常,则您可以更改为@krb686 在评论中提及的内容)

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

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