JSONArray jsonArray= new JSONArray();
//填充初始数据,此处过程省略
List<JSONObject> jsonObjectList = jsonArray.toJavaList(JSONObject.class);
Map<Integer, String> map = jsonObjectList.stream().filter(Objects::nonNull).collect(Collectors.toMap(item -> item.getInteger("flagType"), item -> item.getString("flagIcon")));
JSONArray jsonArray= new JSONArray();
//填充初始数据,此处过程省略
Map<Integer, String> map = jsonArray.stream().filter(Objects::nonNull)
.collect(Collectors.toMap(
object -> {
JSONObject item = (JSONObject) object;
return item.getInteger("flagType");
object -> {
JSONObject item = (JSONObject) object;
return item.getString("flagIcon");
Map<Integer, String> flagIconMap = new HashMap<>();
JSONArray jsonArray= new JSONArray();
//填充初始数据,此处过程省略
if (jsonArray != null && !jsonArray.isEmpty()) {
jsonArray.forEach(object -> {
if (object == null) {
return;
JSONObject jsonObject = (JSONObject) object;
if (jsonObject.getInteger("flagType") == null) {
return;
flagIconMap.put(jsonObject.getInteger("flagType"),jsonObject.getString("flagIcon"));
本文只是自己常用的三种,自己总结一下,不是只有这三种,杠精走开;JSONArray数据[ { "flagType": 1, "flagIcon": "1.jpg" }, { "flagType": 2, "flagIcon": "2.jpg" }, { "flagType": 3, "flagIcon": "3.jpg" }, {
JsonArray数组形式字符串转换为List<Map<String,String>>的8种方法
特别标注:本文主要内容转载自花儿为何那样红博客,博客地址为https://www.cnblogs.com/chancy/p/10179151.html
本文主要用于我们从某个接口地址中获取到的JSON数据,并想将其装换成Map<String, String>类型数据存储于list中。
例如:我们从接口地址中获取到的JSON数据有如下
此时我们需要将body中的userlis
在写代码时,经常会遇到各转类型之间互相转换,比如json转换为Map,jsonArray转List集合,List集合转json,现在整理一个工具类,方便日后查阅。
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apac
数据格式如下,写个对象吧你看看这个数据格式,真麻烦!写个List吧,也是一层一层的!所以决定用map来收取
{"noticeType": "UNIFORM_SUBSCRIPTION",
"notice": { "returnCode": "000000",
"description": "&½",
解决了当一个数据对象模型嵌套另一个数据对象模型的解析困难。
JSON.toJSONString(sendMessage);可以直接进行封装。
JSON.parseObject(xxx);可以指定解析为JSONObject
通过int id = msgJsonObject.getInteger("id");拿到值。
3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject;
5 import java.util.Map;
7 /**
8 * JSON字符串自动转换
JSONArray是Android中用来处理JSON数据格式的类,而List是Java集合框架中的一种数据类型。因此,我们需要将JSONArray转换为List以方便在Java中进行操作。
要将JSONArray转换为List,我们可以按照以下步骤:
1. 首先创建一个ArrayList类型的对象,用来存储JSONArray中的数据。
List<String> list = new ArrayList<String>();
2. 然后使用for循环来遍历JSONArray中的数据,并将其加入到ArrayList中。
JSONArray jsonArray = new JSONArray("[\"apple\",\"banana\",\"orange\"]");
for (int i = 0; i < jsonArray.length(); i++) {
String value = jsonArray.getString(i);
list.add(value);
3. 最后,我们就可以在Java中使用List来处理JSONArray中的数据了。
System.out.println(list);
这样,我们就成功地将JSONArray转换为了List,并且可以利用List中的方法进行后续的操作。