将JsonArray中数据转化为map
时间: 2024-03-15 14:45:56 浏览: 34
可以使用以下代码将一个JsonArray转化为Map:
```java
JsonArray jsonArray = ...; // 获取JsonArray对象
Map<String, JsonObject> map = new HashMap<>();
for (JsonElement element : jsonArray) {
JsonObject jsonObject = element.getAsJsonObject();
String key = jsonObject.get("key").getAsString();
map.put(key, jsonObject);
}
```
在这个代码中,我们首先获取到一个JsonArray对象,然后使用foreach循环遍历其中的每一个JsonElement对象,将其转化为JsonObject,并获取其中的key值作为Map的键,将整个JsonObject作为Map的值。最终,我们就可以得到一个以JsonArray中的key为键,JsonObject为值的Map对象了。
相关问题
将JsonArry中的数据转化为map 的key 和value
可以使用Java中的JSONObject和JSONArray类来实现将JSONArray中的数据转换为Map的key和value,具体实现可以参考下面的代码:
```java
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class JsonArrayToMap {
public static void main(String[] args) {
String jsonArrayStr = "[{\"name\":\"张三\",\"age\":20},{\"name\":\"李四\",\"age\":25}]";
JSONArray jsonArray = new JSONArray(jsonArrayStr);
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
map.put(name, age);
}
System.out.println(map);
}
}
```
上述代码中,我们首先将JSON字符串转换为JSONArray对象,然后遍历JSONArray中的每个JSONObject对象,获取其中的name和age字段值,并将其存入Map中,最后输出Map即可。
JSONArray jsonArray 转 map
要将JSONArray jsonArray转换为Map,有三种方法可以选择:
第一种方法是使用toJavaList方法将JSONArray转换为List<JSONObject>,然后使用stream和collectors.toMap方法将List转换为Map。具体代码如下:
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")));
第二种方法是先创建一个空的HashMap<Integer, String>,然后使用forEach方法遍历JSONArray,将每个JSONObject中的flagType和flagIcon添加到HashMap中。具体代码如下:
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"));
});
}
第三种方法是使用stream和collectors.toMap方法直接将JSONArray转换为Map。具体代码如下:
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");
}
));
以上是三种将JSONArray jsonArray转换为Map的方法,你可以根据自己的需求选择其中一种方法来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [JSONArray转Map的三种实现方式](https://blog.csdn.net/weixin_43075027/article/details/109533803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)