2017-05-28 26 views
-2
func Test_JsonTtransfer(t *testing.T) { 

    uid := "306" 
    phoneList := list.New() 
    phoneList.PushBack("18513622928") 
    fmt.Println("phoneList=======", phoneList.Len()) 


    jsonPhoneList, err := json.Marshal(phoneList) 
    if err != nil { 
     fmt.Println("error:", err) 
    } 
    fmt.Println("jsonPhoneList=======", string(jsonPhoneList)) 

    idCardList := list.New() 
    idCardList.PushBack("230405197608040640") 

    request := make(map[string]interface{}) 
    request["uid"] = uid 

    request["phones"] = phoneList 

    request["id_cards"] = idCardList 

    json, err := json.Marshal(request) 
    if err != nil { 
     fmt.Println("error:", err) 
    } 

    fmt.Println("json=======", json) 
    fmt.Println("json=======", string(json)) 
} 

輸出:爲什麼我轉換映射到JSON,地圖包括列表中的值是什麼轉換後的JSON

d:/系統/服務器/轉到\ BIN \ go.exe測試-v golang-test/com/http/test -run^Test_JsonTtransfer $ phoneList ======= 1 jsonPhoneList ======= {} json ======= [123 34 105 100 95 99 97 114 100 115 34 58 123 125 44 34 112 104 111 110 101 115 34 58 123 125 44 34 117 105 100 34 58 34 51 48 54 34 125] json ======= {「id_cards」:{ },「phones」:{},「uid」:「306」} ok golang-test/com/http/test 0.482s

電話應該是列表值,但沒有。幫我。

回答

1

因爲List類型沒有導出字段,並且類型沒有實現接口,所以列表值總是編組爲{}

一個簡單的解決辦法是使用,而不是一個列表切片:

var phoneList []string 
phoneList = append(phoneList, "18513622928") 
fmt.Println("phoneList=======", len(phoneList) 

playground example

+0

對不起,你的答案是偉大的,但我不在乎的按鈕了。我同意你的想法。 – Zonda

+0

我剛開始使用這個計算器,我應該採用你的建議,但現在我的疏忽,管理員幫我取消我的錯誤選擇 – Zonda

相關問題