技术咨询:如何将JSON Array(数组)转换为Map?
嘿,我来帮你搞定这个JSON数组转Map的问题!首先得看你用的是哪种编程语言,我给你几个主流语言的实现方式:
Java 实现
Java里常用Jackson或Gson这两个JSON处理库来完成转换,这里给你两种方案:
用Jackson库
首先确保你的项目引入了Jackson依赖(比如Maven),然后代码示例如下:
import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import java.util.Map; public class JsonToMapConverter { public static void main(String[] args) throws Exception { // 注意:先把你提供的JSON格式修正(原内容里的key没有引号,是不标准的JSON,要先补全) String jsonArray = "[{\"redirectCount\":0,\"encodedBodySize\":60962,\"unloadEventEnd\":0,\"responseEnd\":1601.699999999255,\"domainLookupEnd\":995.7999999896856,\"unloadEventStart\":0,\"domContentLoadedEventStart\":1970.3999999910593,\"type\":\"navigate\",\"decodedBodySize\":215675,\"duration\":3103.799999997136,\"redirectStart\":0,\"connectEnd\":1357.1999999985565,\"requestStart\":1359.599999996135,\"startTime\":0,\"fetchStart\":1598.6999999877298,\"serverTiming\":[],\"domContentLoadedEventEnd\":1981.999}]"; ObjectMapper objectMapper = new ObjectMapper(); // 将JSON数组解析为List<Map>,每个数组元素对应一个Map List<Map<String, Object>> mapList = objectMapper.readValue(jsonArray, List.class); // 取数组中的第一个元素,就是你要的Map Map<String, Object> resultMap = mapList.get(0); // 打印验证转换结果 resultMap.forEach((key, value) -> System.out.println(key + ": " + value)); } }
用Gson库
如果习惯用Gson,代码逻辑类似:
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.List; import java.util.Map; public class GsonJsonConverter { public static void main(String[] args) { String jsonArray = "[{\"redirectCount\":0,\"encodedBodySize\":60962,\"unloadEventEnd\":0,\"responseEnd\":1601.699999999255,\"domainLookupEnd\":995.7999999896856,\"unloadEventStart\":0,\"domContentLoadedEventStart\":1970.3999999910593,\"type\":\"navigate\",\"decodedBodySize\":215675,\"duration\":3103.799999997136,\"redirectStart\":0,\"connectEnd\":1357.1999999985565,\"requestStart\":1359.599999996135,\"startTime\":0,\"fetchStart\":1598.6999999877298,\"serverTiming\":[],\"domContentLoadedEventEnd\":1981.999}]"; Gson gson = new Gson(); // 通过TypeToken指定泛型类型,避免类型擦除问题 List<Map<String, Object>> mapList = gson.fromJson(jsonArray, new TypeToken<List<Map<String, Object>>>(){}.getType()); Map<String, Object> resultMap = mapList.get(0); } }
Python 实现
Python自带的json模块就能轻松搞定,不需要额外安装依赖:
import json # 修正后的标准JSON字符串 json_array = '''[{"redirectCount":0,"encodedBodySize":60962,"unloadEventEnd":0,"responseEnd":1601.699999999255,"domainLookupEnd":995.7999999896856,"unloadEventStart":0,"domContentLoadedEventStart":1970.3999999910593,"type":"navigate","decodedBodySize":215675,"duration":3103.799999997136,"redirectStart":0,"connectEnd":1357.1999999985565,"requestStart":1359.599999996135,"startTime":0,"fetchStart":1598.6999999877298,"serverTiming":[],"domContentLoadedEventEnd":1981.999}]''' # 解析JSON数组为Python列表,取第一个元素就是字典(相当于Python里的Map) result_map = json.loads(json_array)[0] # 打印验证 print(result_map) # 可以通过键直接取值 print(result_map["type"]) # 输出: navigate
JavaScript/Node.js 实现
JS里JSON和对象的转换非常直观,如果你需要ES6标准的Map对象也可以快速转换:
// 标准JSON字符串 const jsonArray = '[{"redirectCount":0,"encodedBodySize":60962,"unloadEventEnd":0,"responseEnd":1601.699999999255,"domainLookupEnd":995.7999999896856,"unloadEventStart":0,"domContentLoadedEventStart":1970.3999999910593,"type":"navigate","decodedBodySize":215675,"duration":3103.799999997136,"redirectStart":0,"connectEnd":1357.1999999985565,"requestStart":1359.599999996135,"startTime":0,"fetchStart":1598.6999999877298,"serverTiming":[],"domContentLoadedEventEnd":1981.999}]'; // 解析为JS数组,第一个元素是普通对象(可以当作Map使用) const resultObj = JSON.parse(jsonArray)[0]; // 如果需要转换成ES6的Map类型: const es6Map = new Map(Object.entries(resultObj)); // 验证 console.log(resultObj.type); // 输出: navigate console.log(es6Map.get("duration")); // 输出: 3103.799999997136
注意:你提供的原始内容不是标准JSON(key没有加双引号),转换前一定要先修正为标准JSON格式,否则所有解析库都会报错哦!
内容的提问来源于stack exchange,提问作者undetected Selenium
相关产品推荐
相关产品推荐

