FAQ
hi,

I post this earlier also...what I want is to convert the go object in to a
json string using *reflect package*.actually I tried to hard to get the
answer...But I end u with the output as below using reflect package.

*XMLName : {Space: Local:email} *

*Id : 203934839*

*To : {XMLName:{Space: Local:to} Email:john@google.com Name:John Boris}*

*From : {XMLName:{Space: Local:from} Email:me@google.com Name:My Name}*

*Subject : Reminder*

*Body : You need to get grocery before coming home!*


*I used the below code..*



s := reflect.ValueOf(&email).Elem()

         typeOfEmail := s.Type()

         for i := 0; i < s.NumField(); i++ {

             f := s.Field(i)


             fmt.Fprintf(w, " <div>%s : %+v</div><br>", typeOfEmail.Field(i).Name, f.Interface())


         }

But it didn't get the output as a json string.What I need is to output the json string as below...

{
   "email": {
     "-id": "203934839",
     "to": {
       "-email": "john@google.com",
       "#text": "John Boris"
     },
     "from": {
       "-email": "me@google.com",
       "#text": "My Name"
     },
     "subject": "Reminder",
     "body": "You need to get grocery before coming home!"
   }
}

Is it possible using reflect package...?I go through the document about reflect package also...but can't get the correct way...

Please help me


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

  • Brad Fitzpatrick at Jan 23, 2014 at 5:26 am
    Why don't you just use the encoding/json package?

    What's your actual problem? If you just want to learn to use the reflect
    package, you can read encoding/json's source (which uses reflect itself),
    or read http://blog.golang.org/laws-of-reflection etc.


    On Wed, Jan 22, 2014 at 9:01 PM, kate Fernando wrote:

    hi,

    I post this earlier also...what I want is to convert the go object in to a
    json string using *reflect package*.actually I tried to hard to get the
    answer...But I end u with the output as below using reflect package.

    *XMLName : {Space: Local:email} *

    *Id : 203934839*

    *To : {XMLName:{Space: Local:to} Email:john@google.com
    <Email%3ajohn@google.com> Name:John Boris}*

    *From : {XMLName:{Space: Local:from} Email:me@google.com
    <Email%3ame@google.com> Name:My Name}*

    *Subject : Reminder*

    *Body : You need to get grocery before coming home!*


    *I used the below code..*



    s := reflect.ValueOf(&email).Elem()

    typeOfEmail := s.Type()

    for i := 0; i < s.NumField(); i++ {

    f := s.Field(i)


    fmt.Fprintf(w, " <div>%s : %+v</div><br>", typeOfEmail.Field(i).Name, f.Interface())


    }

    But it didn't get the output as a json string.What I need is to output the json string as below...

    {
    "email": {
    "-id": "203934839",
    "to": {
    "-email": "john@google.com",
    "#text": "John Boris"
    },
    "from": {
    "-email": "me@google.com",
    "#text": "My Name"
    },
    "subject": "Reminder",
    "body": "You need to get grocery before coming home!"
    }
    }

    Is it possible using reflect package...?I go through the document about reflect package also...but can't get the correct way...

    Please help me


    --
    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.
  • kate Fernando at Jan 23, 2014 at 5:31 am
    It is for the assignment.I read the all the documents that you
    mentioned...but I don't get the answer that I need after reading them

    --
    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.
  • Tamás Gulácsi at Jan 23, 2014 at 1:18 pm
    Make your email struct's fields exportable (start with uppercase), and
    augment the fields with `json:"subject"` strings to specify names.

    2014. január 23., csütörtök 6:31:18 UTC+1 időpontban kate Fernando a
    következőt írta:

    It is for the assignment.I read the all the documents that you
    mentioned...but I don't get the answer that I need after reading them
    --
    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 Jan 23, 2014 at 1:38 pm

    On 23 January 2014 13:18, Tamás Gulácsi wrote:

    Make your email struct's fields exportable (start with uppercase), and
    augment the fields with `json:"subject"` strings to specify names.
    kate can't use the json package because it's outside the terms
    of her assignment. She's said that at least twice, I think (not in
    those words).

    Kate, you don't want your output to contain <div>s and <br>s; those
    are for HTML. You just need to generate a plain string.

    Your will have a structure that's like this: to convert a struct S

    output a "{"
    for each field F of S
    output the field name and a ":"
    convert the value of the field
    if this isn't the last field, output a ","
    finally output a "}"

    and to convert some value X
    if its a struct, handle it as above
    if its a string, output it in quotes
    if its a number, output it
    otherwise return an error/panic/generate special output
       that says "don't know how to convert (this thing)"

    So "output a structure" is recursive, because it needs to output
    nested structures.

    That should be enough to get you started -- match up what
    you need to do in conversion with what the reflect package
    offers and take it step by step, don't try and solve the whole
    thing in one go, that way madness lies.

    And if you have enough head left, learn how to use Go's
    testing features. They will help you to work out what you're
    trying to do in small steps and to not accidentally throw
    away working code. (Using a version-constrol system like
    mercurial or git also helps.)

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • kate Fernando at Jan 24, 2014 at 3:01 am
    thankyou very much Chris for your help...

    --
    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
postedJan 23, '14 at 5:02a
activeJan 24, '14 at 3:01a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase