FAQ
Is there exiting way to decode `json` without `type` specification ?
probably auto generated with `reflect.TypeOf ` ?

Example :

data := `{"name":"testing"}`
decoded, err := json.Decode(data)
fmt.Println(decoded.Name) // Expects testing




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kyle Lemons at Aug 2, 2013 at 11:50 pm
    If you decode json into an interface{} you'll get out strings, floats,
    map[string]interface{} and []interface{}s, which is as close as you can get
    in a statically compiled language.

    On Fri, Aug 2, 2013 at 2:59 PM, Oleku Konko wrote:

    Is there exiting way to decode `json` without `type` specification ?
    probably auto generated with `reflect.TypeOf ` ?

    Example :

    data := `{"name":"testing"}`
    decoded, err := json.Decode(data)
    fmt.Println(decoded.Name) // Expects testing




    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Oleku Konko at Aug 3, 2013 at 8:07 am
    JSON as limited Data Types ( Int , Float , String , Boolean, map or array ,
    nulll ) than can easily be determined during parsing of the JSON string. GO
    is strictly type but its still has to parse the string.

      I have a JSON string from an application in Java we want to port to golang
    and the structure looks like this :

    Continent -> Country -> Region -> State -> Region -> Local Government ->
    Region -> Area -> Region

    Another Complex Example is from ESPN

    http://espn.go.com/travel/sports/calendar/getList.json?&xhr=1&date=20121027&type=list&query=null&myTeams=

    Or

    http://catalog.viglink.com/vigcatalog/products.json?key=YOUR_KEY&keyword=samsung


    Some of this JSON data can go as deep as 15 - 20 Levels.

    Working with the present decoder makes it so cumbersome for such a simple
    task. I strongly believe that this can be greatly improved.



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Kortschak at Aug 3, 2013 at 10:05 pm
    How does encoding/json not handle those cases?
    On 03/08/2013, at 5:37 PM, "Oleku Konko" wrote:

    Working with the present decoder makes it so cumbersome for such a simple task. I strongly believe that this can be greatly improved.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alonso Harn at Aug 3, 2013 at 5:25 am
    Go can be just as bland as your example, btw, if you wanted to, with a
    helper function:

      data := `{"name":"testing"}`
      decoded, _:= json.Decode(data)
      fmt.Println(decoded["name"]) // Expects testing

    http://play.golang.org/p/z0nVEQVPsk

    I don't recommend it though. =)

    On Friday, August 2, 2013 5:59:31 PM UTC-4, Oleku Konko wrote:

    Is there exiting way to decode `json` without `type` specification ?
    probably auto generated with `reflect.TypeOf ` ?

    Example :

    data := `{"name":"testing"}`
    decoded, err := json.Decode(data)
    fmt.Println(decoded.Name) // Expects testing



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Bronze man at Aug 3, 2013 at 4:14 pm
    I think golang need some easy to use type to handle stuff like this....
    It is too complex to handle type stuff in simple case..

    data := `{"name":"testing"}`
    decoded:= new(interface{})
    json.Unmarshal([]byte(data),decoded)
    decoded1,_:=(*decoded).(map[string]interface{})
    fmt.Println(decoded1["name"]) // Expects testing

    http://play.golang.org/p/GMmv3X7THm

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matt Harden at Aug 3, 2013 at 8:12 pm
    Why don't you want to define a type? Since you know this object will have a
    Name field of type string, why not say so?

    http://play.golang.org/p/AWLPHKx5bA

    package main

    import (
    "encoding/json"
    "fmt"
    "strings"
    )

    func main() {
    data := `{"name":"testing"}`
    decoder := json.NewDecoder(strings.NewReader(data))
    var decoded struct{ Name string }
    decoder.Decode(&decoded)
    fmt.Println(decoded.Name) // Expects testing
    }

    On Friday, August 2, 2013 4:59:31 PM UTC-5, Oleku Konko wrote:

    Is there exiting way to decode `json` without `type` specification ?
    probably auto generated with `reflect.TypeOf ` ?

    Example :

    data := `{"name":"testing"}`
    decoded, err := json.Decode(data)
    fmt.Println(decoded.Name) // Expects testing



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 2, '13 at 11:16p
activeAug 3, '13 at 10:05p
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase