FAQ
I have a struct that I would like to convert to JSON. The struct code is
below. However when I run this piece of code I see only numbers instead.
  I'm using the gorm library within the revel web library ff that makes any
difference. What do you think I'm missing? Thanks in advance

...
var spv SitePageView
spv.Site = &site
db.Connection.Find(&spv.Pages, "site_id = ?", site_id)

json, err := json.Marshal(site)
if err != nil {
panic(err)
}
log.Println(site)
log.Println(json)
...

the site log outputs as expected:
&{3853 3853 {327 true} {77 true} { false} { false} { false} {4 true} {2
true} {General Dentistry true} { false} { false} { false} { false} 1 active
{PayAsYouGo true} 2013-12-01 00:13:38 +0000 UTC 2013-12-01 04:28:26 +0000
UTC}


The json output :

[123 34 73 100 34 58 51 56 53 51 44 34 67 111 109 112 97 110 121 73 100 34
58 51 56 53 51 44 34 84 101 109 112 108 97 116 101 73 100 34 58 123 34 73
110 116 54 52 34 58 51 50 55 44 34 86 97 108 105 100 34 58 116 114 117 101
125 44 34 69 109 114 67 111 109 112 97 110 121 73 100 34 58 123 34 73 110
116 54 52 34 58 55 55 44 34 86 97 108 105 100 34 58 116 114 117 101 125 44
34 69 109 114 76 105 110 107 34 58 123 34 83 116 114 105 110 103 34 58 34
34 44 34 86 97 108 105 100 34 58 102 97...







--
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

  • Ian Davis at Mar 3, 2014 at 6:22 pm
    On Mon, Mar 3, 2014, at 06:09 PM, Alex Casanova wrote:

    I have a struct that I would like to convert to JSON. The struct code
    is below. However when I run this piece of code I see only numbers
    instead. I'm using the gorm library within the revel web library ff
    that makes any difference. What do you think I'm missing? Thanks in
    advance

    ...
    var spv SitePageView
    spv.Site = &site
    db.Connection.Find(&spv.Pages, "site_id = ?", site_id)

    json, err := json.Marshal(site)
    if err != nil {
    panic(err)
    }
    log.Println(site)
    log.Println(json)
    ...

    the site log outputs as expected:
    &{3853 3853 {327 true} {77 true} { false} { false} { false} {4 true} {2
    true} {General Dentistry true} { false} { false} { false} { false} 1
    active {PayAsYouGo true} 2013-12-01 00:13:38 +0000 UTC 2013-12-01
    04:28:26 +0000 UTC}


    The json output :

    [123 34 73 100 34 58 51 56 53 51 44 34 67 111 109 112 97 110 121 73 100
    34 58 51 56 53 51 44 34 84 101 109 112 108 97 116 101 73 100 34 58 123
    34 73 110 116 54 52 34 58 51 50 55 44 34 86 97 108 105 100 34 58 116
    114 117 101 125 44 34 69 109 114 67 111 109 112 97 110 121 73 100 34 58
    123 34 73 110 116 54 52 34 58 55 55 44 34 86 97 108 105 100 34 58 116
    114 117 101 125 44 34 69 109 114 76 105 110 107 34 58 123 34 83 116 114
    105 110 103 34 58 34 34 44 34 86 97 108 105 100 34 58 102 97...



    json.Marshal returns a slice of bytes, which is what those numbers are.

    If you want to print it nicely, convert it to a string:

    log.Println(string(json))

    --
    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.
  • Alex Casanova at Mar 5, 2014 at 3:11 pm
    That worked. Thanks guys
    On Monday, March 3, 2014 1:22:30 PM UTC-5, Ian Davis wrote:




    On Mon, Mar 3, 2014, at 06:09 PM, Alex Casanova wrote:

    I have a struct that I would like to convert to JSON. The struct code is
    below. However when I run this piece of code I see only numbers instead.
    I'm using the gorm library within the revel web library ff that makes any
    difference. What do you think I'm missing? Thanks in advance

    ...
    var spv SitePageView
    spv.Site = &site
    db.Connection.Find(&spv.Pages, "site_id = ?", site_id)

    json, err := json.Marshal(site)
    if err != nil {
    panic(err)
    }
    log.Println(site)
    log.Println(json)
    ...

    the site log outputs as expected:
    &{3853 3853 {327 true} {77 true} { false} { false} { false} {4 true} {2
    true} {General Dentistry true} { false} { false} { false} { false} 1 active
    {PayAsYouGo true} 2013-12-01 00:13:38 +0000 UTC 2013-12-01 04:28:26 +0000
    UTC}


    The json output :

    [123 34 73 100 34 58 51 56 53 51 44 34 67 111 109 112 97 110 121 73 100 34
    58 51 56 53 51 44 34 84 101 109 112 108 97 116 101 73 100 34 58 123 34 73
    110 116 54 52 34 58 51 50 55 44 34 86 97 108 105 100 34 58 116 114 117 101
    125 44 34 69 109 114 67 111 109 112 97 110 121 73 100 34 58 123 34 73 110
    116 54 52 34 58 55 55 44 34 86 97 108 105 100 34 58 116 114 117 101 125 44
    34 69 109 114 76 105 110 107 34 58 123 34 83 116 114 105 110 103 34 58 34
    34 44 34 86 97 108 105 100 34 58 102 97...



    json.Marshal returns a slice of bytes, which is what those numbers are.

    If you want to print it nicely, convert it to a string:

    log.Println(string(json))



    --
    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.
  • DisposaBoy at Mar 3, 2014 at 6:22 pm
    That's the default fmt output for a slice of bytes. Either convert them to strings before printing or use fmt.Printf("%s", ...

    --
    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.
  • Chris dollin at Mar 3, 2014 at 6:25 pm
    marshal returns a slice y of bytes. those are the numbers you are seeing.
    you can convert the slice to a string .

    chris onna bus
    On Mar 3, 2014 6:09 PM, "Alex Casanova" wrote:

    I have a struct that I would like to convert to JSON. The struct code is
    below. However when I run this piece of code I see only numbers instead.
    I'm using the gorm library within the revel web library ff that makes any
    difference. What do you think I'm missing? Thanks in advance

    ...
    var spv SitePageView
    spv.Site = &site
    db.Connection.Find(&spv.Pages, "site_id = ?", site_id)

    json, err := json.Marshal(site)
    if err != nil {
    panic(err)
    }
    log.Println(site)
    log.Println(json)
    ...

    the site log outputs as expected:
    &{3853 3853 {327 true} {77 true} { false} { false} { false} {4 true} {2
    true} {General Dentistry true} { false} { false} { false} { false} 1 active
    {PayAsYouGo true} 2013-12-01 00:13:38 +0000 UTC 2013-12-01 04:28:26 +0000
    UTC}


    The json output :

    [123 34 73 100 34 58 51 56 53 51 44 34 67 111 109 112 97 110 121 73 100 34
    58 51 56 53 51 44 34 84 101 109 112 108 97 116 101 73 100 34 58 123 34 73
    110 116 54 52 34 58 51 50 55 44 34 86 97 108 105 100 34 58 116 114 117 101
    125 44 34 69 109 114 67 111 109 112 97 110 121 73 100 34 58 123 34 73 110
    116 54 52 34 58 55 55 44 34 86 97 108 105 100 34 58 116 114 117 101 125 44
    34 69 109 114 76 105 110 107 34 58 123 34 83 116 114 105 110 103 34 58 34
    34 44 34 86 97 108 105 100 34 58 102 97...







    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 3, '14 at 6:09p
activeMar 5, '14 at 3:11p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase