json error message

843 views
Skip to first unread message

sessetni

unread,
Aug 25, 2010, 4:40:38 PM8/25/10
to golang-nuts
Hello,

I got this error message, "json: cannot unmarshal number into Go value
of type string". And I looked it up the source file
http://golang.org/src/pkg/json/decode.go, what should I do in this
case? thanks a lot.

// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // description of JSON value - "bool", "array",
"number -5"
Type reflect.Type // type of Go value it could not be assigned to
}

func (e *UnmarshalTypeError) String() string {
return "json: cannot unmarshal " + e.Value + " into Go value of
type " + e.Type.String()
}

Russ Cox

unread,
Aug 25, 2010, 4:51:00 PM8/25/10
to sessetni, golang-nuts
> I got this error message, "json: cannot unmarshal number into Go value
> of type string". And I looked it up the source file
> http://golang.org/src/pkg/json/decode.go, what should I do in this
> case? thanks a lot.

Change your program to unmarshal into an integer instead of a string.

Without seeing your program it's very hard to give
a more useful answer.

Russ

Will Walthall

unread,
Aug 25, 2010, 4:51:18 PM8/25/10
to sessetni, golang-nuts
I would assume that this means the Key:value in the json file is of
type number.  And the Key:value used in your go Struct is incorrectly
typed as a string.

sessetni

unread,
Aug 25, 2010, 5:27:31 PM8/25/10
to golang-nuts
I use this program and the atom part is ok:
http://groups.google.com/group/golang-nuts/browse_thread/thread/2cfe3fc5c2b22bb5/278fd8dd439d0800?lnk=gst&q=output+format+json+atom#278fd8dd439d0800

As you can see JTweets struct below, I am not sure where should be
modified.
type JTweets struct {
Results []Result;
Max_id int;
Since_id int;
Refresh_url string;
Next_page string;
Page int;
Completed_in float;
Query string;
}

sessetni

unread,
Aug 25, 2010, 5:56:09 PM8/25/10
to golang-nuts
I have made a few changes and fixed the error. But right now, I got a
new error message, saying "cannot unmarshal null into Go value of type
int", what should I use for nullable type?

for example: "to_user_id":null,


type Result struct {
Profile_image_url string
Created_at string
From_user string
To_user_id string
Text string
Id int // change this from string to int
From_user_id int
Geo string
Iso_language_code string
Source string
}

Russ Cox

unread,
Aug 25, 2010, 6:37:54 PM8/25/10
to sessetni, golang-nuts
On Wed, Aug 25, 2010 at 17:56, sessetni <je...@popularlook.com> wrote:
> I have made a few changes and fixed the error. But right now, I got a
> new error message, saying "cannot unmarshal null into Go value of type
> int", what should I use for nullable type?

I believe you can use *string or interface{}.
The latter admits more things than just string and null.

Russ

sessetni

unread,
Aug 27, 2010, 6:45:59 AM8/27/10
to golang-nuts

On Aug 25, 6:37 pm, Russ Cox <r...@golang.org> wrote:
> I believe you can use *string or interface{}.
> The latter admits more things than just string and null.
>
> Russ

Hello Russ, many thanks!
Reply all
Reply to author
Forward
0 new messages