2017-10-22 12 views
-2

私はjsonをgolangネスト構造体に変換しようとしています。それは部分的にしか働かない。ほとんどのフィールドは正しく解析されませんが、一部は正しく解析されます。なぜ、jsonからのすべてのデータがgolang構造体に変換されないのですか?私の推測では、私のjson形式とgolang構造体の間にいくつかの間違いがありますが、私はそれを見ません。問題について他の目をするための投稿。golang構造体は部分的にjsonからアンマーシャリングされています

私がプログラムを実行すると、マシンのIPアドレスはアンマーシャリングされますが、テスト実行IDは非マーシャルです。ここに私のmainメソッドからの出力は次のようになります。ここでは

test run id: 
machine ip:172.25.148.39 

は私golangコードは次のとおりです。

package main 

import (
    "encoding/json" 
    "bytes" 
    "io/ioutil" 
    "runtime" 
    "log" 
) 

func main() { 
    var testRunConfig TestRunConfig 
    testRunConfigJson := ReadFile("testrun-config.json") 
    err := json.Unmarshal([]byte(testRunConfigJson), &testRunConfig) 
    if err != nil { 
     HandleError(err) 
    } 

    println("test run id:" + testRunConfig.Id) 
    println("machine ip:" + testRunConfig.Machines[0].IP) 
} 

//Logs the error, the function and the line number where it was generated 
func HandleError(err error) (bool) { 
    b := false 

    if err != nil { 
     // notice that we're using 1, so it will actually log the where 
     // the error happened, 0 = this function, we don't want that. 
     pc, fn, line, _ := runtime.Caller(1) 

     log.Printf("[error] in %s[%s:%d] %v", runtime.FuncForPC(pc).Name(), fn, line, err) 
     b = true 
     panic(err) 
    } 

    return b 
} 


func ReadFile(fileName string) string { 
    dat, err := ioutil.ReadFile(fileName) 
    HandleError(err) 
    return string(dat) 
} 

//http://json2struct.mervine.net/ 
type Machine struct { 
    IP   string 
    Interfaces []Interface 
    Containers []Container 
    CmdBuffer bytes.Buffer 
} 

type InetAddress struct { 
    Addr string 
    DelayMS int 
} 

type Interface struct { 
    Name   string 
    InetAddresses []InetAddress 
} 

type Volume struct { 
    Localhost string 
    Volume string 
} 

type NetworkTopology struct { 
    Host string 
    Port string 
} 

type Container struct { 
    Image   string 
    Mem    string 
    Cpu    float64 
    Disk   string 
    Volume   Volume 
    NodeId   int 
    ServiceType  string 
    InstanceId  string 
    Port   string 
    NetworkTopology []NetworkTopology 
    Args   map[string]string 

    //"machine" element being a member of a Container is helpful for code flow/logic, 
    //but is not required as part of the data model (it's duplicate data). 
    //This is why it's private (not capitalized). Think of it as a "transient" in java. 
    machine   Machine 
} 

type TestRunConfig struct { 
    Id  string 
    Machines []Machine 
} 

そしてここでは、変換されるJSONは次のとおりです。あなたのJSONで

{ 
    "Id:": "testRunId", 
    "Machines": [ 
    { 
     "Ip": "172.25.148.39", 
     "Interfaces": [ 
     { 
      "Name": "ems3f0", 
      "InetAddresses": [ 
      { 
       "Addr": "10.0.0.x/16", 
       "DelayMS": 0 
      } 
      ] 
     }, 
     { 
      "Name": "ems3f1", 
      "InetAddresses": [ 
      { 
       "Addr": "10.5.0.x/16", 
       "DelayMS": 5 
      }, 
      { 
       "Addr": "10.15.0.x/16", 
       "DelayMS": 15 
      }, 
      { 
       "Addr": "10.25.0.x/16", 
       "DelayMS": 25 
      } 
      ] 
     } 
     ], 
     "Containers": [ 
     { 
      "Image": "docker-core:1", 
      "Mem": "20GB", 
      "Cpu": 1.0, 
      "Disk": "20GB", 
      "Volume": { 
      "Localhost": "/mnt/md0/${containerId}", 
      "Volume": "/root/.btcd${containerId}" 
      }, 
      "NodeId": 0, 
      "ServiceType": "core", 
      "InstanceId": "core1", 
      "Port": "f(nodeid, serviceid, instanceId -> port #)", 
      "NetworkTopology": [ 
      { 
       "Host": "ip.address", 
       "Port": "some.port" 
      } 
      ], 
      "Args": { 
      "database": "${volume}/init-data.csv.tgz", 
      "listenPort": "1234", 
      "sendPort": "1234" 
      } 
     }, 
     { 
      "Image": "docker-wallet:1", 
      "Mem": "20GB", 
      "Cpu": 1.0, 
      "Disk": "20GB", 
      "Volume": { 
      "Localhost": "/mnt/md0/${containerId}", 
      "Volume": "/root/.btcd${containerId}" 
      }, 
      "NodeId (cluster)": 0, 
      "ServiceType": "wallet", 
      "InstanceId": "1...100", 
      "Port": "portNum", 
      "Args": { 
      "Database": "${volume}/init-data.csv.tgz", 
      "ListenPort": "1234", 
      "SendPort": "1234" 
      } 
     } 
     ] 
    } 
    ] 
} 

答えて

2

、あなたが持っていますタイプミス(余分なコロン)。 "Id"ではなく、問題のフィールド"Id:"を呼び出します。 :を削除すれば、コードはうまくいきます!

NodeIdフィールドは、JSONでは"NodeId"の代わりに"NodeId (cluster)"と表示されるため、入力されない可能性があります。フィールド名はJSONとGoで正確に一致する必要があります。 JSONでGoとは異なるフィールド名を使用する場合は、構造体にJSON名を付ける:

関連する問題