2017-03-03 3 views
-1
{ 
    "devices": [ 
    { 
     "id": 20081691, 
     "targetIp": "10.1.1.1", 
     "iops": "0.25 IOPS per GB", 
     "capacity": 20, 
     "allowedVirtualGuests": [ 
     { 
      "Name": "akhil1" 
     }, 
     { 
      "Name": "akhil2" 
     } 
     ] 
    } 
    ] 
} 

このJSONデータの構造表現を記述して、デバイスをリストに追加したり削除したりするにはどうすればいいですか?私はさまざまな構造表現を試しましたが、何も動作していません。以下は、同様の種類のjsonデータで試した例の1つです。私はそれに新しいデータを追加することができません。構造表現とAPPENDはあなたがjson:"id"のような構造体タグを使用することができ、ここでgolangで次のjsonデータの構造表現はどのようになりますか?

package main 

import (
    "encoding/json" 
    "fmt" 
) 

type Person struct { 
    ID  string `json:"id,omitempty"` 
    Firstname string `json:"firstname,omitempty"` 
    Lastname string `json:"lastname,omitempty"` 
    Address []Address `json:"address,omitempty"` 
} 

type Address[] struct { 
    City string `json:"city,omitempty"` 

} 


func main() { 

var people []Person 
people = append(people, Person{ID: "1", Firstname: "Nic", Lastname: "Raboy", Address: []Address{{City: "Dublin"},{ City: "CA"}}}) 
b, err := json.Marshal(people) 
    if err != nil { 
     fmt.Println("json err:", err) 
    } 
    fmt.Println(string(b)) 
} 

答えて

0

それは以下になります。しかし、あなたは読みやすくするために小さな構造体にそれを破ることができることを簡素化するために

type MyStruct struct { 
    Devices []struct { 
     ID     int `json:"id"` 
     TargetIP    string `json:"targetIp"` 
     Iops     string `json:"iops"` 
     Capacity    int `json:"capacity"` 
     AllowedVirtualGuests []struct { 
      Name string `json:"Name"` 
     }       `json:"allowedVirtualGuests"` 
    }        `json:"devices"` 
} 

:これは、優れたJSON-to-GOツールを使用して生成されました。例は次のとおりです。

package main 

import "fmt" 

type VirtualGuest struct { 
    Name string `json:"Name"` 
} 

type Device struct { 
    ID     int   `json:"id"` 
    TargetIP    string   `json:"targetIp"` 
    Iops     string   `json:"iops"` 
    Capacity    int   `json:"capacity"` 
    AllowedVirtualGuests []VirtualGuest `json:"allowedVirtualGuests"` 
} 

type MyStruct struct { 
    Devices []Device `json:"devices"` 
} 

func main() { 

    var myStruct MyStruct 

    // Add a MyStruct 
    myStruct.Devices = append(myStruct.Devices, Device{ 
     ID:1, 
     TargetIP:"1.2.3.4", 
     Iops:"iops", 
     Capacity:1, 
     AllowedVirtualGuests:[]VirtualGuest{ 
      VirtualGuest{ 
       Name:"guest 1", 
      }, 
      VirtualGuest{ 
       Name:"guest 2", 
      }, 
     }, 
    }) 

    fmt.Printf("MyStruct: %v\n", myStruct) 
} 
+0

ありがとうございました。 AllowedVirtualGuests [] VirtualGuest –

+0

の代わりに[AllowedVirtualGuests]フィールドを[]構造体として定義しても同じことができますか。既存の回答を参照してください:http://stackoverflow.com/questions/32531854/how-to-initialize-nested-structure-array-in-golang – keno

0

間違っている可能性があります行われている方法は、以下のストラクト試してみてください。

type Data struct { 
     Devices []struct { 
       Id     int `json:"id"` 
       IP     string `json:"targetIp"` 
       IOPS     string `json:"iops"` 
       Capacity    int `json:"capacity"` 
       AllowedVirtualGuests []struct { 
         Name string `json:"Name"` 
       } `json:"allowedVirtualGuests"` 
     } `json:"devices"` 
} 
+0

ありがとうございます。しかし、どのように新しいデバイスのJSONデータを追加するのですか? –

+0

デバイスを構造体に追加し、マーシャリングします。 – zzn

+0

上記のコードで使用されているappendメソッドを使用してこれを行うことができます。 –

関連する問題