Search code examples
jsongogo-iris

How to parse json post request


I want to post json data to Go api, But i can't parse json in Go

javascript code:

data= {"user":{"username":"admin","password":"123"},"profile":{"firstname":"morteza","lastname":"khadem","files":["/temp/a.jpg","/temp/b.jpg"]}}

$.post('/parse-json', data, function () {
    alert('success');
});

in php get data very simple ($_REQUEST['user']['firstname']) but in Go different


Solution

  • Now i use this code:

    type Merchant struct{}
    
    func (*Merchant) Register(context context.Context){
        type registerRequestData struct{
            Merchant models.MrtMerchant `json:"merchant"`
            User models2.UsrUser `json:"user"`
            Profile models2.UsrUserProfile `json:"profile"`
            Branch models.MrtMerchantBranch `json:"branch"`
        }
        var request registerRequestData
        if err:=context.ReadJSON(&request);err!=nil{
            panic(err)
        }
    
        fmt.Printf("%+v\n",request)
    }