How to parse JSON array in Go

The return value of Unmarshal is an error, and this is what you are printing out:

// Return value type of Unmarshal is error.
err := json.Unmarshal([]byte(dataJson), &arr)

You can get rid of the JsonType as well and just use a slice:

package main

import (
    "encoding/json"
    "log"
)

func main() {
    dataJson := `["1","2","3"]`
    var arr []string
    _ = json.Unmarshal([]byte(dataJson), &arr)
    log.Printf("Unmarshaled: %v", arr)
}

// prints out:
// 2009/11/10 23:00:00 Unmarshaled: [1 2 3]

Code on play: https://play.golang.org/p/GNWlylavam

Background: Passing in a pointer allows Unmarshal to reduce (or get entirely rid of) memory allocations. Also, in a processing context, the caller may reuse the same value to repeatedly – saving allocations as well.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)