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

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 6 | next ›
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