如何HashMap中转换成JSON数组中的android? [英] How to convert HashMap to json Array in android?

查看:1203
本文介绍了如何HashMap中转换成JSON数组中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换HashMap来JSON数组我的code是如下:

 地图<字符串,字符串>图=新的HashMap<字符串,字符串>();

map.put(第一,第一个值);

map.put(第二,秒值);
 

我也试过,但没有奏效

  JSONArray mJSONArray =新JSONArray(Arrays.asList(图));
 

解决方案

试试这个,

 公开的JSONObject(地图的copyfrom)
 

从给定的地图复制所有名称/值映射关系创建一个新的JSONObject。

参数 具有copyfrom一个映射,其键是String类型,其值是支持的类型。

抛出 NullPointerException - 如果任何地图上的按键都为空。

基本用法:

 的JSONObject的obj =新的JSONObject(yourmap);
 

从的JSONObject得到JSON数组

编辑:

好,我会告诉

  JSONArray阵列=新JSONArray(obj.toString());
 

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

Map<String, String> map = new HashMap<String, String>();

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());

这篇关于如何HashMap中转换成JSON数组中的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆