gpt4 book ai didi

json - 从 API 反序列化 JSON 值 - 大括号与方括号

转载 作者:IT王子 更新时间:2023-10-29 06:44:20 25 4
gpt4 key购买 nike

我正在努力整合来自 API 的一些天气信息,我从中获得了这样的返回值......

{"coord":{"lon":-85.6,"lat":43.05},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":277.33,"pressure":1010,"humidity":65,"temp_min":276.15,"temp_max":279.15},"visibility":16093,"wind":{"speed":5.7,"deg":100,"gust":8.2},"clouds":{"all":75},"dt":1524076680,"sys":{"type":1,"id":1410,"message":0.0043,"country":"US","sunrise":1524048860,"sunset":1524097768},"id":420018526,"name":"Grand Rapids","cod":200}

我已经创建了一个 built_valuebuilt_collection 对象来反序列化。如果我“手动”构建对象并将其序列化,然后对它进行 JSON 编码...

var w = new Welcome((b) => b
..id = 420018526
..name = "Grand Rapids"
..weather.add(new Weather((w)=>w
..id = 803
..main = "Clouds"
..description = "broken clouds"
..icon = "04d"
))
..cod = 200
..coord.lat = 43.05
..coord.lon= -85.6
..base = 'stations'
..main.temp = 277.33
..main.pressure = 1010
..main.humidity = 65
..main.temp_min = 276.15
..main.temp_max = 279.15
..visibility = 16093
..wind.speed = 5.7
..wind.deg = 100
..wind.gust = 8.2
..clouds.all = 75
..dt = 1524076680
..sys.type = 1
..sys.id = 1410
..sys.message = 0.0043
..sys.country = "US"
..sys.sunrise = 1524048860
..sys.sunset = 1524097768
);

var ws = serializers.serialize(w);
var wsj = JSON.encode(ws);

我得到了这样的东西......

["Welcome","coord",["lon",-85.6,"lat",43.05],"weather",[["id",803,"main","Clouds","description","broken clouds","icon","04d"]],"base","stations","main",["temp",277.33,"pressure",1010,"humidity",65,"temp_min",276.15,"temp_max",279.15],"visibility",16093,"wind",["speed",5.7,"deg",100,"gust",8.2],"clouds",["all",75],"dt",1524076680,"sys",["type",1,"id",1410,"message",0.0043,"country","US","sunrise",1524048860,"sunset",1524097768],"id",420018526,"name","Grand Rapids","cod",200]

... 看起来非常相似,但您会注意到 dart:convert JSON 库似乎更喜欢方括号 [ 而不是花括号 {

当我尝试反序列化大括号时 wsjdA = JSON.decode(wJson); 我得到一个错误

type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Welcome' of 'wsjdA' where
_InternalLinkedHashMap is from dart:collection
String is from dart:core
Welcome is from package:ex_models/src/weather.dart

但是如果我做这个愚蠢的事情(解析方括号为大括号并添加第一个字符串“Welcome”...

var wJsonB= '["Welcome",' + wJson
.replaceAll('{', '[')
.replaceAll('}', ']')
.replaceAll(':', ',')
.substring(1);

...它工作正常

我是否遗漏了什么 - 是否有更简单的方法在 dart 中执行 JSON/对象反序列化。我真的很想能够以标准方式将有效的 JSON API 响应解析为 dart 对象,而无需手动解析。

如果不是针对 Root/Welcome 对象中的 Weather 对象列表,我认为这可能有效。因为这个对象包含一个对象列表,这有点令人困惑?这是我的对象中的一个 BuiltList

我已经在使用 built_value 对象和 built_collections 并使用 dart:convert 库。想知道我是否遗漏了什么?

最佳答案

[] 用于列表/数组,{} 用于 map 。通过 built_value,您可以使用 BuiltListBuiltMap 类型的属性。

JSON.decode(wJson); 不返回类的实例,它只是从 JSON 字符串中生成列表、映射和分离原始值。

built_value 有它自己的序列化,从 JSON.decode(...) 返回到类的具体实例。

built_value 默认情况下具有与您的 JSON 不兼容的简化 JSON 格式。为此,您需要注册 StandardJsonPlugin (另见 https://github.com/google/built_value.dart/issues/171)

有关使用 built_value 进行序列化的详细信息,请参阅 https://medium.com/dartlang/darts-built-value-for-serialization-f5db9d0f4159 .

关于json - 从 API 反序列化 JSON 值 - 大括号与方括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926959/

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