2017-05-01 2 views
0

GolangのJSONファイルを読み込み、このJSONファイルを変更してから、新しいJSONファイルを作成したり、このJSONファイルに書き込もうとしています。私はオンラインでいくつかのサンプルを見てきましたが、必要な結果を得るために2つと2つを一緒に置くことはできません。私は自分のJSON strをGOで作成してみましたが、まだ失敗しました。私は以下のファイルを読み込むと、いくつかの試みを行ったGolangを使用してJSONファイルを変更する

package main 

import (
    "encoding/json" 
    "fmt" 
) 

type Person struct { 
    Name string 
    Age  int 
    Details interface{} 
} 

func main() { 
    //I created a simple Json structure here to play with 
    str := `{"name": "A", 
     "age":20, 
     "details": {"salary":100000000} 
    }` 
    var data Person 

    err := json.Unmarshal([]byte(str), &data) 
    if err != nil { 
     panic(err) 
    } 

    //I make a map so that I can adjust the value of the salary 
    details, ok := data.Details.(map[string]interface{}) 
    if ok { 
     details["salary"] = 999999 
    } 
    //Change the other values of a Person 
    data.Name = "B" 
    data.Age = 19 
    fmt.Println(data) 

    //SAMPLE OUTPUT: {B 19 map[salary:999999]} 
} 

は私の最高の試みです:

package main 

import (
    "encoding/json" 
    "fmt" 
    "io/ioutil" 
    "os" 
) 

/* Create the structure that follows the outline of the JSON file*/ 

type UserType struct { 
    User []struct { 
     CdbID  string `json:"cdb_id"` 
     Firstname string `json:"firstname"` 
     Lastname string `json:"lastname"` 
     Phone  int64 `json:"phone"` 
     Email  string `json:"email"` 
     Address []struct { 
      Street string `json:"street"` 
      City string `json:"city"` 
      Zip  string `json:"zip"` 
      Country string `json:"country"` 
     } `json:"address"` 
     Authenticators []struct { 
      Name string `json:"name"` 
      Phone int64 `json:"phone"` 
     } `json:"authenticators"` 
     VoiceSig   string `json:"voice_sig"` 
     VoicesigCreatedTime string `json:"voicesig_created_time"` 
     Status    string `json:"status"` 
    } `json:"user"` 
} 

func main() { 
    file, err := ioutil.ReadFile("user.json") //Read File 
    if err != nil { 
     fmt.Print("Error:", err) 
    } 

    var u UserType 
    json.Unmarshal(file, &u)  //Parse the Json-encoded Data and store the results in u 
    result, e := json.Marshal(u) //Returns the Json encoding of u into the variable result 
    if e != nil { 
     fmt.Println("error", err) 
    } 
    os.Stdout.Write(result) //The line of code golang.org uses to print the Json encoding 
} 

これはサンプル出力です:

{ 
    "user": [{ 
     "cdb_id":"", 
     "firstname":"Tom", 
     "lastname":"Bradley", 
     "phone":14155555555, 
     "email":"[email protected]", 
     "address":[{ 
      "street":"4343 shoemaker ave", 
      "city":"Brea", 
      "zip":"92821", 
      "country":"USA" 
     }], 
     "authenticators":[{ 
      "name":"Lisa Hayden", 
      "phone":15625555555 
     },{ 
      "name":"Pavan M", 
      "phone":17145555555 
     }], 
     "voice_sig":"242y5-4546-555kk54-437879ek545", 
     "voicesig_created_time":"2017-08-02T21:27:44+0000", 
     "status":"verified" 
    }] 
} 

私はどのように上だけで混乱しています私が欲しいもの、特に上記のサンプル出力の "Authenticators"を修正してください。ありがとう!

答えて

1

宣言

type Authenticator struct { 
    Name string `json:"name"` 
    Phone int64 `json:"phone"` 
} 

を追加し、最初のユーザーにオーセンティケータを追加するために、次に

Authenticators []Authenticator `json:"authenticators"` 

Authenticators []struct { 
     Name string `json:"name"` 
     Phone int64 `json:"phone"` 
} `json:"authenticators"` 

を変更:

u.User[0].Authenticators = append(u.User[0].Authenticators, Authenticator{ 
     Name: "John Doe", 
     Phone: 1234567890, 
}) 
+0

私はあなたのコードを理解していますが、私がうんざりしていない限り、何かがうまくいっていません。私はエラーを取得しています: "#コマンドライン引数 ./json.go:48:u.User.Authenticatorsは未定義です"。 UserTypeの構造​​を変更し、そのテキストブロックをより細かい構造のより細かいシリーズに置き換えるのではなく、ありがとう、私はあなたの応答を評価する! –

+0

私は 'u.User'がスライスであることを忘れていました。上記のコードを修正しました。 – md2perpe

関連する問題