- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个结构体正在与 protobuff 序列化器一起使用并且运行良好。
这个结构是由 protobuff 生成的,因此它有很多方法,比如 Unmarshal 等。
type Flight struct {
FlightNo string `json:"flightno, omitempty"`
Carrier string `json:"carrier, omitempty"`
}
func (m *Flight) Unmarshal(data []byte) error {
l := len(data)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowFlight
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := data[iNdEx]
iNdEx++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
}
// some more code
}
然后我想为这个航类信息添加额外的字段,
type FlightMeta struct {
Source string `json:"source, omitempty"`
Destination string `json:"destination, omitempty"`
}
然后我结合了这两个结构,
type CombinedFlight struct {
Flight
FlightMeta
}
type ResponseFlight struct {
OnwardFlights []CombinedFlight `json:"onwardflights, omitempty"`
ReturnFlights []CombinedFlight `json:"returnflights, omitempty"`
Error string `json:"error, omitempty"`
}
所以当我读取一些数据时,比如,
str "= `{"onwardflights": [{"flightno": "537", "carrier": "AI", "source": "BOM", "destination": "DEL"}], "error": "false"}`
respFlight = new(ResponseFlight)
err = json.Unmarshal([]byte(str), &respFlight)
fmt.Println("flightno:", respFlight.OnwardFlights[0].FlightNo, "flight source", respFlight.OnwardFlights[0].Source)
#prints "flightno:537 flight source:
它不打印第二个结构的值,因为按照我的说法,这没有正确解码。
但是当我手动设置属性并编码(将其编码为 json)时,这工作正常。有线索吗?
最佳答案
尝试这样做
type CombinedFlight1 struct {
Flight
FlightMeta
}
type CombinedFlight2 struct {
Flight
FlightMeta
}
type ResponseFlight struct {
OnwardFlights []CombinedFlight1 `json:"onwardflights, omitempty"`
ReturnFlights []CombinedFlight2 `json:"returnflights, omitempty"`
Error string `json:"error, omitempty"`
}
它会很好地工作,如果你想编码,你必须创建两个不同的结构。如需更多了解,请参阅这篇文章
访问Initialize nested struct definition in Golang if it have same objects
关于json - Golang : Protobuff generated Struct is not decoding child attribute for json. 解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39851428/
我有一个数组 items[] items[] 中的每一项都是一个结构体。 item 有键 id、date、value(即 item.id、item.date、item.value) 我想使用 Stru
我想存储 100 名员工。 RollNo,姓名,工资,时间(各种数据,我无法在这里解释,但你可以看下面的代码片段来理解 main() { struct day { int hour
这个问题在这里已经有了答案: storage size of ‘names’ isn’t known (3 个答案) 关闭 5 年前。 我正在尝试蓝牙编程,遇到了这个我不明白的问题。基本上,当我使用
这是一个奇怪的事情: 我有一个结构,它包含指向相同类型结构的指针和指向其他类型结构的指针,以及一些其他值。 struct animal { struct animal * father;
我有一个结构定义如下(名称不同) struct str1 { int field1; struct str2; } 我在一个函数中有一个*str1。我想要一个指向 str2 的指针。 所以
DISK_DETECTION_INFO is defined as有什么原因吗? typedef struct _DISK_DETECTION_INFO { DWORD Size
我正在尝试打包一个字符串和一个字符串的长度。 fmt = '
我在创建结构时遇到问题。 我的结构: public struct Device: Codable { let data: DeviceData let meta: Meta? } pu
struct Item { var name:String? var type:String? var value:Int? var tag:Int? } ... ..
// NewReaderSize returns a new Reader whose buffer has at least the specified 43 // size. If the ar
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 3 年前。 在下面的 C++ 片段中, 如何基于 TwoInts 结构中的
#include struct Header { unsigned long long int alignment; }; int main(void) { struct Heade
我有一个目前看起来像这样的结构(缩写为仅显示基本部分): typedef struct { uint32_t baudrate; ... some other internally u
对此没有太多解释,这就是我所拥有的: public struct PACKET_HEADER { public string computerIp; publi
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
struct S1 { char c; int i; }; struct S3 { char c1; struct S1 s; double c2; }; 我正
我有一个名为 Parameter 的协议(protocol): protocol Parameter { var name: String { get } var unit: Unit
有 2 个 struct 定义 A 和 A。我知道 struct A 可以包含指向 struct A 的 POINTER 但我不明白为什么 struct A 不能包含struct A(不是指针) 最佳
我有以下代码: struct MyStruct{ data: &'a str, } fn get(S: &'a MyStruct) -> &'a str{ S.data } fn se
为了说明这一点,这里有一个小的不可变结构和一个更新它的函数: (struct timeseries (variable observations) #:transparent) (define (ad
我是一名优秀的程序员,十分优秀!