why does this JSON code fail to print the name after unmarshalling?

152 views
Skip to first unread message

Chris Dawson

unread,
Jun 19, 2014, 12:13:54 PM6/19/14
to golan...@googlegroups.com
This code correctly determines that there are three items in the JSON, but fails to unmarshal the values. Can someone help me understand why?

package main
import (
        "encoding/json"
        "fmt"
)
var the_json = `                                                                                                                         
 [{"description":"","is_official":false,"is_trusted":true,"name":"foobar/blah","star_count":1}                                           
,{"description":"","is_official":false,"is_trusted":true,"name":"yucks/dog","star_count":0}                                              
,{"description":"","is_official":false,"is_trusted":true,"name":"yonkers/bonkers","star_count":0}                                        
]                                                                                                                                        
`
type Item struct {
        description string
        is_official bool
        is_trusted bool
        name string
        star_count int
}

func main() {
        var items []Item
        json.Unmarshal([]byte(the_json), &items)
        for _, di := range items {
                fmt.Println( "Name: " + di.name + " starred #" + string(di.star_count) + " times" )
        }
}

Caleb Spare

unread,
Jun 19, 2014, 12:26:24 PM6/19/14
to Chris Dawson, golang-nuts
That doesn't look like valid JSON. But you're completely
ignoring the error from json.Unmarshal. If it's not nil, then items
should not be used.
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

chris dollin

unread,
Jun 19, 2014, 12:29:11 PM6/19/14
to Chris Dawson, golan...@googlegroups.com

Your fields are not exported, so they won't be written to.

Chris onna bus.

--

Harmen B

unread,
Jun 19, 2014, 12:32:54 PM6/19/14
to chris dollin, Chris Dawson, golan...@googlegroups.com
On Thu, Jun 19, 2014 at 6:28 PM, chris dollin <ehog....@googlemail.com> wrote:

Your fields are not exported, so they won't be written to.


For example:

(note that the 'string(di.start_count)' also doesn't make a string of an int)

Chris Dawson

unread,
Jun 19, 2014, 12:34:46 PM6/19/14
to golan...@googlegroups.com, xrda...@gmail.com, ehog....@googlemail.com
Caleb: actually, it is valid JSON, handling error did not break the code, and I figured it was getting somewhere since it figured out there were three items in the JSON array. Invalid JSON would not get this far, right?

Chris, that makes sense. I changed the fields to start with all caps, and that works for the di.Name, but cannot figure out how to get star_count translated. I tried "Star_count", "Star_Count" and "StarCount" without success.

Chris

Chris Dawson

unread,
Jun 19, 2014, 12:35:39 PM6/19/14
to golan...@googlegroups.com, ehog....@googlemail.com, xrda...@gmail.com
Harmen,

Awesome, thank you.

Chris

Shawn Milochik

unread,
Jun 19, 2014, 1:10:05 PM6/19/14
to golan...@googlegroups.com
On Thu, Jun 19, 2014 at 12:34 PM, Chris Dawson <xrda...@gmail.com> wrote:
Chris, that makes sense. I changed the fields to start with all caps, and that works for the di.Name, but cannot figure out how to get star_count translated. I tried "Star_count", "Star_Count" and "StarCount" without success.


(re-send of post send only to Chris Dawson)

Instead of:

    StarCount int

try something like: 

    StarCount int `json:"star_count"`
 

Johann Höchtl

unread,
Jun 19, 2014, 5:36:39 PM6/19/14
to golan...@googlegroups.com
Out of curiosity: are you i OGD/CKAN?
Reply all
Reply to author
Forward
0 new messages