Tutorial by Examples: decoding

The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
Occasionally you will find the need to encode binary data as a base64-encoded string. For this we can use the DatatypeConverter class from the javax.xml.bind package: import javax.xml.bind.DatatypeConverter; import java.util.Arrays; // arbitrary binary data specified as a byte array byte[] bi...
If you need to get data from a JSONObject, consider the following example: String json = "{\"foo\":\"bar\",\"temperature\":21.5,\"year\":2016,\"message\":{\"Hello\":\"world\"},\"months\":[\"January\",\&qu...
The json_decode() function takes a JSON-encoded string as its first parameter and parses it into a PHP variable. Normally, json_decode() will return an object of \stdClass if the top level item in the JSON object is a dictionary or an indexed array if the JSON object is an array. It will also retur...
// decode NSString *string = [[NSString alloc] initWithData:utf8Data encoding:NSUTF8StringEncoding]; // encode NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding]; Some supported encodings are: NSASCIIStringEncoding NSUTF8StringEnc...
.encode and .decode both have error modes. The default is 'strict', which raises exceptions on error. Other modes are more forgiving. Encoding >>> "£13.55".encode('ascii', errors='replace') b'?13.55' >>> "£13.55".encode('ascii', errors='ignore') b'13.55' ...
json.Unmarshal from the package "encoding/json" decodes a JSON value into the value pointed by the given variable. The parameters are the value to decode in []bytes and a variable to use as a storage for the de-serialized value. The returned value is an error (on failure). encodedValue :...
XML elements often nest, have data in attributes and/or as character data. The way to capture this data is by using ,attr and ,chardata respectively for those cases. var doc = ` <parent> <child1 attr1="attribute one"/> <child2>and some cdata</child2> </p...
JSON data can also be read from files. Let's assume we have a file called data.json with the following content: [ { "Name" : "John Doe", "Standard" : 4 }, { "Name" : "Peter Parker", "Standard" : ...
Encoding //Create a Base64 Encoded NSString Object NSData *nsdata = [@"iOS Developer Tips encoded in Base64" dataUsingEncoding:NSUTF8StringEncoding]; // Get NSString from NSData object in Base64 NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0]; // Print the B...
The goal with using anonymous structs is to decode only the information we care about without littering our app with types that are used only in a single function. jsonBlob := []byte(` { "_total": 1, "_links": { "self": "https://api.twitch.tv/k...
The following example can be tested on https://ellie-app.com/m9tk39VpQg/0. import Html exposing (..) import Json.Decode payload = """ ["fu", "bar"] """ main = Json.Decode.decodeString decoder payload -- Ok ["fu","b...
Encoding and decoding functions for a variety of Unicode encodings can be found in the Data.Text.Encoding module. ghci> import Data.Text.Encoding ghci> decodeUtf8 (encodeUtf8 "my text") "my text" Note that decodeUtf8 will throw an exception on invalid input. If you wa...
encoding := base64.StdEncoding data := []byte(`Zm9vIGJhcg==`) decoded := make([]byte, encoding.DecodedLen(len(data))) n, err := encoding.Decode(decoded, data) if err != nil { log.Fatal(err) } // Because we don't know the length of the data that is encoded // (only the max length), we n...
decoded, err := base64.StdEncoding.DecodeString(`biws`) if err != nil { log.Fatal(err) } fmt.Printf("%s", decoded) // Output: n,, Playground
The easiest and quickest way to encode a Haskell data type to JSON with Aeson is using generics. {-# LANGUAGE DeriveGeneric #-} import GHC.Generics import Data.Text import Data.Aeson import Data.ByteString.Lazy First let us create a data type Person: data Person = Person { firstName :...
Always encode from unicode to bytes. In this direction, you get to choose the encoding. >>> u'🐍'.encode('utf-8') '\xf0\x9f\x90\x8d' The other way is to decode from bytes to unicode. In this direction, you have to know what the encoding is. >>> b'\xf0\x9f\x90\x8d'.decode('...
This is useful if you use rust in the backend and elm on the front end enum Complex{ Message(String), Size(u64) } let c1 = Complex::Message("hi"); let c2 = Complex::Size(1024u64); The encoded Json from rust will be: c1: {"variant": "Message", ...
Let's assume we have the following struct that defines a City type: type City struct { Name string Temperature int } We can encode/decode City values using the encoding/json package. First of all, we need to use the Go metadata to tell the encoder the correspondence between the...
The following code can be found in a demo here: https://ellie-app.com/mbFwJT9jD3/0 import Html exposing (..) import Json.Decode exposing (Decoder) payload = """ [{ "id": 0, "name": "Adam Carter", "work": "Uni...

Page 1 of 2