Hello,
I’m trying to generate fairly nested JSON request data. I’m in trouble figuring out, how to generate multiple specific objects against []int.
Playgroud link: https://play.golang.org/p/U2thXB3saKO
Thanks in advance!
Margus
Hello,
I’m trying to generate fairly nested JSON request data. I’m in trouble figuring out, how to generate multiple specific objects against []int.
Playgroud link: https://play.golang.org/p/U2thXB3saKO
Thanks in advance!
Margus
You can use a custom integer type that knows how to marshal itself oddly:
Ideally this is what yields the results you are looking for. I am not sure about how to use calmh’s idea here. Please keep me posted if you can
request := &Request{
JsonHead: “4.0”,
JsonID: 1,
Method: “read”,
Params: Param{
Objects: []Object{
{ObjID: objectIds[0]},{ObjID: objectIds[1]},{ObjID: objectIds[2]},
},
},
}
Hi.
Thanks for fast reply.
calmh: I’m not sure, how could I use oddInt function in this situation. It is returning already marshaled []byte and I should be marshaling static and this dynamic objects json data separately and join them after marshaling into needed request which I think is too much hassle.
I would like to use method given by javapriyan, but I don’t know how to add these objects dynamically. There may be more than three objectIds in slice of ints.
Thanks!
Margus
You can dynamically add the objects with:
request := &Request{
JsonHead: "4.0",
JsonID: 1,
Method: "read",
}
for _, oid := range objectIds {
request.Params.Objects = append(request.Params.Objects, Object{ObjID: oid})
}
https://play.golang.org/p/fFBUOMvPbik does this and checks the output.
nathankerr, Thank you very much! This was exactly what I needed. It’s simple and elegant solution. I can continue moving forward with my little project.
As I presumed, I still have lots to learn about data types and how to use them properly.
Thanks for all who answered!
I just showed marshalling them as is. The MarshalJSON solution works fine if you embed an oddInt anywhere in your object as well. But go with whatever works.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.