Golang Revel to dynamically receive JSON post data under a non-declared object

Source: Internet
Author: User
Tags node server

Used to write the node server API, change to static type language very uncomfortable, rules a lot ~

to be dynamic required!!!

Here are a few bags to note

JSON conversion and parsing

Github.com/bitly/go-simplejson

Go get Github.com/bitly/go-simplejson


Body conversion parsing with direct import

Stringsio/ioutil


Postman Submitting data

{"Data": "Yes", "Hotel": ["Caonima", "Gelebi"], "HP": 10000000}

Controller action data Post receive

package controllersimport  (     "Github.com/revel/revel"      " Io/ioutil "    " strings "    console " FMT "     Simplejson "Github.com/bitly/go-simplejson"  )  type Hotels struct {     *revel. controller} type dct map[string]interface {}func  (C hotels)  Index ()   Revel. Result {    if strings. Contains (c.request.contenttype,  "Application/json")  {         content, _ := ioutil. ReadAll (c.request.body)         cn_json, _ := simplejson . Newjson ([]byte (content))         console. Println (Cn_json)         cn_body, _ := cn_json. Get ("Data"). String ()      &nbsP;  cn_hotel, _ := cn_json. Get ("Hotel"). Array ()         cn_hp, _ := cn_json. Get ("HP"). Int ()         console. Println (CN_HP)         console. Println (cn_body)         for _, v := range cn_ Hotel {         console. Println ("V", v)         }    }     tt:=Dct{         "name": "Brother Chun",          "Properties":D ct{"pet": "Grass Martini", "Skill": []string{"Domineering Spring brother Fist", "Resurrection in situ"}},         }    return c.renderjson (TT)}


Golang Revel to dynamically receive JSON post data under a non-declared object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.