2016-03-31 9 views
0

私は過去数日間、初めてGOさんと実験しています。JSONをデコードする際のエラー

私はその値をサーバーに渡すHTMLフォームを持っています。このサーバーは、フォームキーと値を抽出し、JSON内に配置します。このJSONは別のサーバーに送信されます。

問題は次のとおりです。第二のサーバはJSONをデコードしようとしたとき、私は次のエラーを取得する:

Error decoding JSON: json: cannot unmarshal string into Go value of type main.NewContainerJSON

1:元のHTMLフォーム

<form method="post" action="http://127.0.0.1:8080/new-user" autocomplete ="on"> 
<table> 
    <tr> 
     <td colspan="2"><h1>Container Configuration</h1></td> 
    </tr> 
    <tr> 
     <td><h2>Container Name</h2></td> 
     <td><input type="text" name="containerName" placeholder = "My Container Name" required /></td> 
    </tr> 
    <tr> 
     <td><h2>Base Server</h2></td> 
     <td> 
      <select name="BaseServer"> 
       <option value="Ubuntu 14.04">Ubuntu 14.04</option> 
     </td> 
    </tr> 
    <tr> 
     <td><h2>Content Management System</h2></td> 
     <td> 
      <select name="CMS"> 
       <option value="Wordpress">Wordpress</option> 
     </td> 
    </tr> 
    <tr> 
     <td><h2>Website Name</h2></td> 
     <td><input type="text" name="websiteName" placeholder = "mysite.com" required /></td> 
    </tr> 
    <tr> 
     <td><h2>New Root Database Password</h2> </td> 
     <td><input type = "password" name = "dbRootPWD" placeholder = "password" required /></td> 
    </tr> 
    <tr> 
     <td><h2>Database Admin Username</h2></td> 
     <td><input type = "text" name = "dbAdminUname" placeholder = "Admin" required /></td> 
    </tr> 
    <tr> 
     <td><h2>Database Admin Password</h2></td> 
     <td><input type = "password" name = "dbAdminPwd" placeholder = "password" required /></td> 
    </tr> 
    <tr> 
     <td></td> 
     <td><input type = "submit" value = "submit"></td> 
    </tr> 
</table>  

2 :最初のサーバーコード

package main 

import (
"fmt" 
"encoding/json" 
"net" 
"net/http" 
) 

type newContainerJSON struct { 
    ContainerName string 
    BaseServer string 
    CMS string 
    WebsiteName string 
    DBrootPWD string 
    DBadminUname string 
    DBadminPWD string 
} 

func newUser(w http.ResponseWriter, r *http.Request) { 
    r.ParseForm() 

    cName := r.FormValue("containerName") 
    sName := r.FormValue("BaseServer") 
    cmsName := r.FormValue("CMS") 
    wsName := r.FormValue("websiteName") 
    dbrootPwd := r.FormValue("dbRootPWD") 
    dbadmName := r.FormValue("dbAdminUname") 
    dbamdpwdName := r.FormValue("dbAdminPwd") 

    c := newContainerJSON { 
     ContainerName: cName, 
     BaseServer: sName, 
     CMS: cmsName, 
     WebsiteName: wsName, 
     DBrootPWD: dbrootPwd, 
     DBadminUname: dbadmName, 
     DBadminPWD: dbamdpwdName, 
    } 

    d, _ := json.Marshal(c) 
    s := string(d) 
    fmt.Println(s) 

    conn, err := net.Dial("tcp", "127.0.0.1:8081") 
    checkError(err) 

    encoder := json.NewEncoder(conn) 

    encoder.Encode(d) 
} 

func main() { 
    http.HandleFunc("/new-user", newUser) 
    err := http.ListenAndServe(":8080", nil) // setting listening port 
    checkError(err) 
} 

func checkError(err error) { 
    if err != nil { 
     fmt.Println("Fatal error ", err.Error()) 
    } 
} 

3:第二のサーバコード:

package main 

import (
    "fmt" 
    "net" 
    "encoding/json" 
) 

type NewContainerJSON struct { 
    ContainerName string `json:",string"` 
    BaseServer string  `json:",string"` 
    CMS string    `json:",string"` 
    WebsiteName string  `json:",string"` 
    DBrootPWD string  `json:",string"` 
    DBadminUname string  `json:",string"` 
    DBadminPWD string  `json:",string"` 
} 

func main() { 

    service := "127.0.0.1:8081" 
    tcpAddr, err := net.ResolveTCPAddr("tcp", service) 
    checkError(err) 

    listener, err := net.ListenTCP("tcp", tcpAddr) 
    checkError(err) 

    conn, err := listener.Accept() 
    checkError(err) 

    decoder := json.NewDecoder(conn) 

    var b NewContainerJSON 
    err = decoder.Decode(&b) 
    checkError(err) 

    fmt.Println(b) 

    conn.Close() // we're finished 

} 

func checkError(err error) { 
    if err != nil { 
     fmt.Println("An error occurred: ", err.Error()) 

    } 
} 

エラーが第二のサーバコード

var b NewContainerJSON 
err = decoder.Decode(&b) 
checkError(err) 

fmt.Println(b) 

に次のコードを使用して発生し、私は私が正しくJSONをデコードしていないですか、私は非常に何かが欠けてい疑います明らかです。

答えて

3

最初のサーバーは値を2重にエンコードしています。結果は文字列です。

d, _ := json.Marshal(c) // d is []byte containing the JSON 
... 
encoder.Encode(d) // encoder writes base64 encoding of []byte as JSON string 

変更するコード:あなたがencoder.Encode(d)を行うと、前のステップからのマーシャリング結果をコードしている

conn, err := net.Dial("tcp", "127.0.0.1:8081") 
if err != nil { 
    // handle error 
} 
encoder := json.NewEncoder(conn) 
if err := encoder.Encode(c); err != nil { 
    // handle error 
} 
+0

おかげで百万、:

これは、あなたが理解するのに役立つはずです。 –

1

。だからあなたがそれをデコードすると、あなたはgoオブジェクトを取得するのではなく、文字列を取得します。

代わりに、encoder.Encode(c)とすることができます。 (オブジェクトcを直接エンコードする)。完全に働いたhttp://play.golang.org/p/qNxqOJcj_a

+0

乾杯、ありがとう... –

関連する問題