gpt4 book ai didi

json - Dart:将 Map 转换为 JSON 并引用所有元素

转载 作者:行者123 更新时间:2023-12-03 21:37:25 25 4
gpt4 key购买 nike

我将 Dart 中的表单序列化为 JSON,然后使用 Jackson 将其发布到 Spring MVC 后端以反序列化 JSON。

在 Dart 中,如果我打印出 JSON,我会得到:

{firstName: piet, lastName: venter}

Jackson 不喜欢这种格式的数据,它返回状态 400 和 The request sent by the client was syntactically incorrect.
如果我在所有字段周围加上引号,Jackson 会接受数据并得到回复。
{"firstName": "piet", "lastName": "venter"}

在 Dart 中,我构建了一个 Map<String, String> data = {};然后遍历所有表单字段并执行 data.putIfAbsent(input.name, () => input.value);
现在当我调用 data.toString() ,我得到未引用的 JSON,我猜它是无效的 JSON。

如果我 import 'dart:convert' show JSON;并尝试 JSON.encode(data).toString();我得到相同的未引用 JSON。

手动附加双引号似乎有效:
data.putIfAbsent("\"" + input.name + "\"", () => "\"" + input.value + "\"");

在 Java 方面没有火箭科学:
@Controller
@RequestMapping("/seller")
@JsonIgnoreProperties(ignoreUnknown = true)
public class SellerController {

@ResponseBody
@RequestMapping(value = "/create", method = RequestMethod.POST, headers = {"Content-Type=application/json"})
public Seller createSeller(@RequestBody Seller sellerRequest){

所以我的问题是,在 Dart 中是否有一种不那么骇人听闻的方式来构建 Jackson 期望的引用 JSON(除了手动转义引号和手动添加引号)?
可以将 Jackson 配置为允许未引用的 JSON 吗?

最佳答案

import 'dart:convert';
...
json.encode(data); // JSON.encode(data) in Dart 1.x

总是为我引用 JSON。
您无需调用 toString()

关于json - Dart:将 Map 转换为 JSON 并引用所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29294019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com