I received the uint8 array json style data and print it as strings, it shown like: AADAfwAAwH8AAMB/AAAAAAAAwH8AAMB/AADAfwAAAAAAAMB/AADAfwAAwH8AAAAAAADAfwAAwH8AAMB/AAAA
It should be some numbers, so I don't know how to decode it, it looks like Base64 but actually I cannot decode like that.
What language?
C# to decode from JSON to bytes:
byte[] bytes = Encoding.Default.GetBytes(json);
C++ to decode from JSON to bytes:
(uint8*)TCHAR_TO_UTF8(json)
Related
This question already has an answer here:
Convert byte slice to io.Reader
(1 answer)
Closed 2 months ago.
I have a body response that I can only get Byte responses. This bytes encode a csv-like response. Something like:
element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active
Lets say then that I have the struct that looks like this:
type ESResponse struct {
ElementA string `csv:"element_a"`
ElementB string `csv:"element_b"`
ElementC string `csv:"element_c"`
}
I would like to unmarshal the byte response so then I'm able to access its elements.
What I've been doing is the following:
var actualResult ESResponse
body := util.GetResponseBody() // this is where the byte response comes from.
in := string(body[:]) // here I transform it to a string but I trully think this is not the best way.
err = gocsv.Unmarshal(in, &actualResult)
I've been using this library here: https://pkg.go.dev/github.com/gocarina/gocsv#section-readme but I'm unable to understand the error I get which is:
cannot use in (variable of type string) as io.Reader value in argument to gocsv.Unmarshal: string does not implement io.Reader (missing method Read)
It means, that in argument must implement interface io.Reader, but you argument's type is string, which doesn't. So if you want to deserialize value from string, you can do this:
body := `
element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active`
var actualResult []ESResponse
in := strings.NewReader(body)
err := gocsv.Unmarshal(in, &actualResult)
or gocsv.Unmarshal(bytes.NewReader([]byte(body)), &actualResult) to deserialize from bytes array
Is this possible way to add nested json "as is". The nested json doesn't have any structure and might be different. I need to put the nested json data directly to the root node.
https://play.golang.org/p/MzBt7DLQEpD
type RootJson struct {
NestedJson []byte
AdditionalField string
}
func main() {
nestedJson := []byte("{\"number\": 1, \"string\": \"string\", \"float\": 6.56}")
rootJson := RootJson{nestedJson, "additionalField"}
payload, _ := json.Marshal(&rootJson)
fmt.Println(string(payload))
}
Yes, it's possible. Use the json.RawMessage type which implements custom marshaling / unmarshaling, which "renders" it as-is into the JSON output. It's just a plain byte slice:
type RawMessage []byte
Its value should be the UTF-8 encoded byte sequence of the raw JSON text (exactly what you get when you do a conversion, e.g. []byte("someText")).
type RootJson struct {
NestedJson json.RawMessage
AdditionalField string
}
With this, the output will be (try it on the Go Playground):
{"NestedJson":{"number":1,"string":"string","float":6.56},
"AdditionalField":"additionalField"}
(Indentation added by me.)
How can i save the SwiftyJSON String array into String of Array in swift
let jsonStr: JSON = JSON(socketDictionary.myStructValue)
print(jsonStr)
The output is:
["1CZ3isxgnmrY8w1eUJmCjNSm2fvhfXYkNQ","12ZKF9uZnaz5Wk6wVvmphE42LjM5M51upV","1LzPBqfrsdxDeFkeZqSGuaS3zxw8wTRQXx","1Z5qe2NSZyzvoNbmU7tuB7AaH7i4JQJXJ","15BKJq6Q2QHdw8v93JwBqy5GKrbLDWbbUR","1DCVJrzfZrFxqBGRF3H744SB1dC8aw5Rgn","1QGsCPM2HLfAMyFdfWddzijbzXF6cwm7RA","1QGW6T2KLTuWuu5qkfmZ8E63amtZmasRSu","1CLSGF92T8936tfm9qKG4N6cQir7AKSM7Q","1C7wnv7TBaX3H3xeHycr95h9mzzvVUq9Wz","1vBegRfLZbiwyEZ9LXo4d1KbLnUEyZLpF","1Nh9mxHuXF2T6V8qwHf95T6G24EFaQRG2M","16S2TgTX4ArtNW2htUCdCk6KvAZZFfw8Xi","1J3f1UoAGwmbK1aN9vTYex5rjpDnDewtTH","1BsawvNcAs289FZv3LbZY6STweTVJk6DeW","1LSQBT7QuBBSkwyFZpsxoEuw3oBDe57DT1","1LUSG4Pm7FiiCBUcEjWduLsCjswPheHZyF","1KPD18LDsmemHUR8TyLc8C3LJRKTgxVFe5","1FASJSpNjLVTEqmM72KjrZ85H7GudbQxZn","1MZxxP5YjLh29W7tgcrtYc2ywuAjdB2FC4","1JakuQCqpcmz1husavN8dHFGQyy3yDRCrD","177D2KQ4gd6kW1b4jTs9TLjUAV927F6Wc4","1PgKwdsKBqFNsZz4JcZ82kqBXRpCbWjPae","1P2iYhmVxhg3W8tEeQQK23NY5mKxHPbNnQ","14AgWBp6XYw12cBdAvdwz3sh9issWX7UnW","1DMPZjYf9LvHe3p7ZvZTVYsasGJps5QWWu","1MncjTfvdkNp1P1z22LAgbXU8mmTi59sLw","1PLLzt7Jiww6KqfsRwM6aW4z6pHoJNiDLR","1JvGhkZ26hy1Tjmxyx8C9amzo3pt6Ecxfh","1D3P1oYE9PBJhz9HYXeVJwuM5Qgg6RdYRu","1DGaE7K1nPjoVuL7BD4y3NWZvQ8gGNzenj","1h81XFB5nLsRJu2UtPinJx3niv1LMWSj5","18tAQ6awwcecSbxT63kQBmyJm7hWQLDppe","17p9vLfD1Q3Hdxp5zCNuX4cSe7TDjvzDXc","1GAsmp3j26Y6AkMG1jtVoZFBxZnJMSWQLF","1MJa3EFHFJwY2myibcVWiB6zuofFFaBWAR","19FwYXp1vfgU7bNojsggkuVZsiiAKRxbF7","18tG7Czk2akY2qbcn651CwCWzv9QA3DKUn","1AzacPufLFp7tL593Yimg7cnu4SymganbL","1EifaT43avdWcsXMhyVHvPmtNz7YyfDEnZ","17Xax4sY5Z694QQEKKz1ioaSrxXujnoa1y","18G4HWStJBVE4k4uLnif5cKAsyXpKx3VqY","1FUKc1zh8ia2h1j8Z1DqTTVQ2cJSyx7AnW","1HQq9mQxePKZg8eDncggx9LZ3ufc6sYSPp","14mKUH5jFmEW6DiKAiGJmeztqZ46XQkxch","1JWJwbiPzehXz8D5s7jTddEDxtMLBaFfkV","1GZbEdUjBhqZfp1JAyaoCZRTCiXSa4gbty","1D69wSHc1wK778gYWogzrm2hnerCBs1ZC6","12aa3sB1DTQDGT5UQHWnU71ziLLvQHKLTr","1AF2rxRKJbtUTNH4iyX5csrrG66Y723YwP","1AR77rXiZga2XUd2W7Wk1jZQfGjtK3gDBs","1PVJqkWLEed11uHHBeH3x4ya6zSjS7sv2e","168VzjYSz3tYaYLLoF28WKoc2NzBTAM866","13SnzBJL7jrHfPCWSAS8GD4ctmKAfgvTaa","16KBupqzu8hUCzqjdgr6ZFfM9TFnRkz82u","1Pd7NY5onBAQtwjf9aKGaun8QDhuoFf7jr","1LM65ijpT6SnTwQHd6bz44RZAaaVXEmT8x","1Mzg7bLFoA5wrDT2gUk7M9JPA7yxGYGN7c","1N7Tw2KDaUWRexGCoNSa5ecZkDHdvNRw1Z","1GPRVhFa1X99VqYJuRR6wBP1vz1txWiNYQ","1JaaeGaNeytwTPh9Dwv9XdcUyfzmhDVi6a"]
i want to save this json output array to simple array and then iterate those array
Just use dicionaryObject from SwiftyJSON. This will convert JSON value into [String: Any].
Ex.: jsonStr.dicionaryObject
I need to pass an array of string/Int (doesnt matter) as JSON parameter in a HTTP body for a POST request.
{
"par1" : value,
"par2" : "value2",
"par3" : ['123:456', '123:234' ...]*
}
*My problem is for filling my JSON object with param3 values. In the swift code I have them as an array of Strings.
There are many example of converting a JSON object back to an array of strings/Int, but I can't find it the other way around.
As mentioned in the comments, the way to go is to use NSJSONSerialization to generate object encoded in JSON data.
I'm trying to convert json string in to xml format using Golang. My whole object is to convert json in to map string interface and then convert interface in to xml file. (There is no predefined structs). Please help me to solve this issue?
var f interface{}
err := json.Unmarshal(b, &f)
There's an xml package that is equivalent to the json one under encoding. Just import it and then marshal the result of your json.Unmarshal
import "encoding/xml"
xml.Marshal(&f)