2016-02-08 39 views
0

我在golang很新,但我很努力..golang,陶醉,如何解析後json?

我試圖通過發送請求給酒吧控制器,並解析它在狂歡的一面json。

不過,雖然得到的結果,我不能解組......我要送一個數組

json_encode(array("one","two","three")) 

但我不能找到這樣的數據一起工作的正確途徑。我不知道我是否需要發送與否之前進行JSON ..

func (c KpiCtrl) GetData() revel.Result { 
    content, _ := ioutil.ReadAll(c.Request.Body) 
    ... 
    return c.RenderJson(content) 
    } 

回報

"WyJvbmUiLCJ0d28iLCJ0aHJlZSJd" 

我試圖用json.Unmarshal但它返回的錯誤..什麼是最好的練習使用curl發送的發佈數據來陶醉控制器?

+0

第一:檢查和處理你的錯誤。第二:你用json.Unmarshal的代碼是什麼? – elithrar

+0

'log.Printf(「%q」,content)'show?唯一編碼爲base64字符串的JSON是'[] byte''。你忘了先解碼嗎? –

+0

我嘗試過使用json.Unmarshal([] byte(c.Request.Body),&myStruct),但它表示不能將..io.Reader轉換爲[] byte .... – Altenrion

回答

4

只需使用非標準json解碼器:

var content []string 
    err := json.NewDecoder(c.Request.Body).Decode(&content) 
    if err != nil { 
     log.Fatal("JSON decode error: ", err) 
    } 
    defer c.Request.Body.Close() 
    fmt.Println(content) 
+0

已測試您的答案,作品像一個魅力。謝謝,古茹Gopher =)。 – Altenrion