json objects

126 views
Skip to first unread message

Hamsa Hesham

unread,
Dec 26, 2020, 10:06:22 AM12/26/20
to golang-nuts
how to unmarshal more than one json object  .. 

Here is the code 
data, err := ioutil.ReadFile("file.txt")
    checkError(err)
    x:=string(data)
    fmt.Println(x)
    
    var b1 book
    err1:=json.Unmarshal(data,&b1)
    
    fmt.Printf("Books : %+v", b1)
    checkError(err1)
    if b1.ID==ID {
        log.Printf("The book with ID:%d has title: %s, it was publiched at date: %s,his author is: %s,it's genre is: %s,it was published by: %s,it's language is: %s.",b1.ID,b1.Title,b1.PubDate,b1.Author,b1.Genre,b1.Publisher,b1.Language)
    }else{
        fmt.Println("There is no such book!")
    }
that's the output if the file has one object 
Screenshot (54).png

it doesn't work if there are many objects
Screenshot (53).png

Charles Radke

unread,
Dec 26, 2020, 10:15:59 AM12/26/20
to Hamsa Hesham, golang-nuts

I think you probably to unmarshal into a []book.

 

 

 

From: <golan...@googlegroups.com> on behalf of Hamsa Hesham <hamsah...@gmail.com>
Date: Saturday, December 26, 2020 at 10:06 AM
To: golang-nuts <golan...@googlegroups.com>
Subject: [go-nuts] json objects

 

how to unmarshal more than one json object  .. 

 

Here is the code 

data, err := ioutil.ReadFile("file.txt")

    checkError(err)

    x:=string(data)

    fmt.Println(x)

    

    var b1 book

    err1:=json.Unmarshal(data,&b1)

    

    fmt.Printf("Books : %+v", b1)

    checkError(err1)

    if b1.ID==ID {

        log.Printf("The book with ID:%d has title: %s, it was publiched at date: %s,his author is: %s,it's genre is: %s,it was published by: %s,it's language is: %s.",b1.ID,b1.Title,b1.PubDate,b1.Author,b1.Genre,b1.Publisher,b1.Language)

    }else{

        fmt.Println("There is no such book!")

    }

that's the output if the file has one object 

 

it doesn't work if there are many objects

 

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7888bad2-c4b0-4f64-915c-3ad5d60f18fan%40googlegroups.com.

Martin Schnabel

unread,
Dec 26, 2020, 10:37:06 AM12/26/20
to golan...@googlegroups.com
hi hamsa,

first of all: it is usually frowned upon to send text as pictures. so
please copy text as text in the future. the are people with terminal
mail clients or visually impaired using screen readers.

the data in your example is neither valid json value nor a valid json
stream. either you use a json array [{"book":…},{…}] surrounded by
square brackets and missing the trailing comma and unmarshal it into a
[]Book or []*Book. or you use a json stream where multiple json values
follow each other separated usually be a new line and creating a
json.NewDecoder and calling it multiple times to read all books until
you receive a io.EOF error.

have fun and happy holidays

On 26.12.20 16:06, Hamsa Hesham wrote:
> how to unmarshal more than one json object  ..
>
> Here is the code
> data, err := ioutil.ReadFile("file.txt")
>     checkError(err)
>     x:=string(data)
>     fmt.Println(x)
>     var b1 book
>     err1:=json.Unmarshal(data,&b1)
>     fmt.Printf("Books : %+v", b1)
>     checkError(err1)
>     if b1.ID==ID {
>         log.Printf("The book with ID:%d has title: %s, it was publiched at date: %s,his author is: %s,it's genre is: %s,it was published by: %s,it's language is: %s.",b1.ID,b1.Title,b1.PubDate,b1.Author,b1.Genre,b1.Publisher,b1.Language)
>     }else{
>         fmt.Println("There is no such book!")
>     }
> that's the output if the file has one object
> Screenshot (54).png
>
> it doesn't work if there are many objects
> Screenshot (53).png
>
> --
> 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
> <mailto:golang-nuts...@googlegroups.com>.
> <https://groups.google.com/d/msgid/golang-nuts/7888bad2-c4b0-4f64-915c-3ad5d60f18fan%40googlegroups.com?utm_medium=email&utm_source=footer>.

钟晓键

unread,
Dec 26, 2020, 12:49:33 PM12/26/20
to golang-nuts
In fact, the JSON content you pasted here is NOT valid, cause there shouldn't be more than one root element in a single JSON file (you can try online JSON validator to validate your JSON file).

Amnon

unread,
Dec 28, 2020, 3:37:37 AM12/28/20
to golang-nuts
You could try something like this 

https://play.golang.org/p/a-4i045QdXX
Reply all
Reply to author
Forward
0 new messages