在Dart与Flutter中提取指定JSON字段转换为Map的方法
提取JSON指定字段并转换为Map的实现方法
下面针对几种主流编程语言,给出具体实现方案:
Java
方式1:使用 Jackson 库
先确保项目中引入Jackson依赖:
<!-- Maven 依赖 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency>
代码实现:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; public class JsonExtractor { public static void main(String[] args) throws Exception { String jsonStr = "{\"_id\":\"_gz-cfmqA1mr\",\"name\":\"Alexander Pope\",\"link\":\"https://en.wikipedia.org/wiki/Alexander_Pope\",\"bio\":\"Alexander Pope (21 May 1688 – 30 May 1744) is regarded as one of the greatest English poets...\",\"description\":\"English poet\",\"quoteCount\":3,\"slug\":\"alexander-pope\",\"dateAdded\":\"2020-12-19\",\"dateModified\":\"2020-12-19\"}"; ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonStr); Map<String, Object> resultMap = new HashMap<>(); resultMap.put("name", jsonNode.get("name").asText()); resultMap.put("quoteCount", jsonNode.get("quoteCount").asInt()); resultMap.put("slug", jsonNode.get("slug").asText()); // 输出结果 System.out.println(resultMap); } }
方式2:使用 Gson 库
引入Gson依赖:
<!-- Maven 依赖 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency>
代码实现:
import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.util.HashMap; import java.util.Map; public class GsonJsonExtractor { public static void main(String[] args) { String jsonStr = "{\"_id\":\"_gz-cfmqA1mr\",\"name\":\"Alexander Pope\",\"link\":\"https://en.wikipedia.org/wiki/Alexander_Pope\",\"bio\":\"Alexander Pope (21 May 1688 – 30 May 1744) is regarded as one of the greatest English poets...\",\"description\":\"English poet\",\"quoteCount\":3,\"slug\":\"alexander-pope\",\"dateAdded\":\"2020-12-19\",\"dateModified\":\"2020-12-19\"}"; JsonObject jsonObject = JsonParser.parseString(jsonStr).getAsJsonObject(); Map<String, Object> resultMap = new HashMap<>(); resultMap.put("name", jsonObject.get("name").getAsString()); resultMap.put("quoteCount", jsonObject.get("quoteCount").getAsInt()); resultMap.put("slug", jsonObject.get("slug").getAsString()); System.out.println(resultMap); } }
Python
Python自带的json模块即可完成,无需额外依赖:
import json json_str = ''' { "_id": "_gz-cfmqA1mr", "name": "Alexander Pope", "link": "https://en.wikipedia.org/wiki/Alexander_Pope", "bio": "Alexander Pope (21 May 1688 – 30 May 1744) is regarded as one of the greatest English poets...", "description": "English poet", "quoteCount": 3, "slug": "alexander-pope", "dateAdded": "2020-12-19", "dateModified": "2020-12-19" } ''' # 解析JSON为字典 data = json.loads(json_str) # 提取指定字段到新字典(Python中字典等价于Map) result_map = {key: data[key] for key in ['name', 'quoteCount', 'slug']} print(result_map)
JavaScript/Node.js
在JS中,解析后的JSON对象本身就可以当作Map使用,也可以转为标准Map类型:
const jsonStr = `{ "_id": "_gz-cfmqA1mr", "name": "Alexander Pope", "link": "https://en.wikipedia.org/wiki/Alexander_Pope", "bio": "Alexander Pope (21 May 1688 – 30 May 1744) is regarded as one of the greatest English poets...", "description": "English poet", "quoteCount": 3, "slug": "alexander-pope", "dateAdded": "2020-12-19", "dateModified": "2020-12-19" }`; // 解析JSON const data = JSON.parse(jsonStr); // 方式1:提取为普通对象 const resultObj = { name: data.name, quoteCount: data.quoteCount, slug: data.slug }; // 方式2:转换为标准Map类型 const resultMap = new Map([ ['name', data.name], ['quoteCount', data.quoteCount], ['slug', data.slug] ]); console.log(resultObj); console.log(resultMap);
内容的提问来源于stack exchange,提问作者Abhijeet Saroha
相关产品推荐
相关产品推荐

