2016-03-13 61 views
-1

所以,我有一些麻烦,在golang解析这些数据:解析JSON在GoLang到结构

{ 
"gateways": [ 
    { 
     "token": "my_token_here", 
     "gateway_type": "test", 
     "description": null, 
     "payment_methods": [ 
      "credit_card", 
      "sprel", 
      "third_party_token", 
      "bank_account", 
      "apple_pay" 
     ], 
     "state": "retained", 
     "created_at": "2016-03-12T18:52:37Z", 
     "updated_at": "2016-03-12T18:52:37Z", 
     "name": "Spreedly Test", 
     "characteristics": [ 
      "purchase", 
      "authorize", 
      "capture", 
      "credit", 
      "general_credit", 
      "void", 
      "verify", 
      "reference_purchase", 
      "purchase_via_preauthorization", 
      "offsite_purchase", 
      "offsite_authorize", 
      "3dsecure_purchase", 
      "3dsecure_authorize", 
      "store", 
      "remove", 
      "disburse", 
      "reference_authorization" 
     ], 
     "credentials": [], 
     "gateway_specific_fields": [], 
     "redacted": false 
    } 
] 

}

当使用这个结构,我可以得到它的输出非常容易。

type gateways struct { 
    Gateways []struct { 
     Characteristics  []string  `json:"characteristics"` 
     CreatedAt    string  `json:"created_at"` 
     Credentials   []interface{} `json:"credentials"` 
     Description   interface{} `json:"description"` 
     GatewaySpecificFields []interface{} `json:"gateway_specific_fields"` 
     GatewayType   string  `json:"gateway_type"` 
     Name     string  `json:"name"` 
     PaymentMethods  []string  `json:"payment_methods"` 
     Redacted    bool   `json:"redacted"` 
     State     string  `json:"state"` 
     Token     string  `json:"token"` 
     UpdatedAt    string  `json:"updated_at"` 
    } `json:"gateways"` 
} 

但只要我单独的“网关[]结构”到自己的结构,然后返回一个空数组...

全部来源。

type gateway struct { 
    Characteristics  []string  `json:"characteristics"` 
    CreatedAt    string  `json:"created_at"` 
    Credentials   []interface{} `json:"credentials"` 
    Description   interface{} `json:"description"` 
    GatewaySpecificFields []interface{} `json:"gateway_specific_fields"` 
    GatewayType   string  `json:"gateway_type"` 
    Name     string  `json:"name"` 
    PaymentMethods  []string  `json:"payment_methods"` 
    Redacted    bool   `json:"redacted"` 
    State     string  `json:"state"` 
    Token     string  `json:"token"` 
    UpdatedAt    string  `json:"updated_at"` 
} 
type gateways struct { 
    Gateways []gateway `json:"gateways"` 
} 

func ParseResponse() { 
    var parsed gateways 
    json.Unmarshal(json, &parsed) 
} 
+0

你必须有一些问题,在你的代码,因为它为我工作。 https://play.golang.org/p/AjeKfc-JTH –

回答

0

您的ParseResponse功能出现问题,您打电话给json.Unmarshal作为第一个参数json,这是一个包装名称:这是不明确的。你可以看到,你的代码很好地改变了ParseResponse函数。

package main 

import (
    "encoding/json" 
    "fmt" 
) 

type gateway struct { 
    Characteristics  []string  `json:"characteristics"` 
    CreatedAt    string  `json:"created_at"` 
    Credentials   []interface{} `json:"credentials"` 
    Description   interface{} `json:"description"` 
    GatewaySpecificFields []interface{} `json:"gateway_specific_fields"` 
    GatewayType   string  `json:"gateway_type"` 
    Name     string  `json:"name"` 
    PaymentMethods  []string  `json:"payment_methods"` 
    Redacted    bool   `json:"redacted"` 
    State     string  `json:"state"` 
    Token     string  `json:"token"` 
    UpdatedAt    string  `json:"updated_at"` 
} 

type gateways struct { 
    Gateways []gateway `json:"gateways"` 
} 

func ParseResponse(js []byte) { 
    var parsed gateways 
    json.Unmarshal(js, &parsed) 
    fmt.Println(parsed) 
} 

func main() { 
    var js []byte = []byte(`{ 
"gateways": [ 
    { 
     "token": "my_token_here", 
     "gateway_type": "test", 
     "description": null, 
     "payment_methods": [ 
      "credit_card", 
      "sprel", 
      "third_party_token", 
      "bank_account", 
      "apple_pay" 
     ], 
     "state": "retained", 
     "created_at": "2016-03-12T18:52:37Z", 
     "updated_at": "2016-03-12T18:52:37Z", 
     "name": "Spreedly Test", 
     "characteristics": [ 
      "purchase", 
      "authorize", 
      "capture", 
      "credit", 
      "general_credit", 
      "void", 
      "verify", 
      "reference_purchase", 
      "purchase_via_preauthorization", 
      "offsite_purchase", 
      "offsite_authorize", 
      "3dsecure_purchase", 
      "3dsecure_authorize", 
      "store", 
      "remove", 
      "disburse", 
      "reference_authorization" 
     ], 
     "credentials": [], 
     "gateway_specific_fields": [], 
     "redacted": false 
    } 
] 
}`) 
    /* 
     var parsed gateways 
     e := json.Unmarshal(js, &parsed) 
     if e != nil { 
      fmt.Println(e.Error()) 
     } else { 
      fmt.Println(parsed) 
     } 
    */ 
    ParseResponse(js) 
} 

输出:

{[{[purchase authorize capture credit general_credit void verify reference_purchase purchase_via_preauthorization offsite_purchase offsite_authorize 3dsecure_purchase 3dsecure_authorize store remove disburse reference_authorization] 2016-03-12T18:52:37Z [] <nil> [] test Spreedly Test [credit_card sprel third_party_token bank_account apple_pay] false retained my_token_here 2016-03-12T18:52:37Z}]} 
0

看看http://play.golang.org/p/3xJHBmhuei - 解组到您的网关的第二个定义成功完成,所以它必须是小东西,你缺少的。

检查json.Unmarshal调用是否返回错误。

P.S.这可能不是问题,如果你解组成第一个版本的网关,但你上面给出的JSON字符串缺少关闭的“}”括号。