2017-07-28 6 views
-1

私は主にgolangで以下のJSON配列のアンマーシャリングを検討していました。ネストされたJson Arrayはゴランの非マーシャリングを構成します

{ 
"status":{"code":"SUCCESS"}, 
"result": { 
       "total_records":1, 
       "records": [{ 
          "last_modified_timestamp":1501209015807, 
          "dns_servers":null, 
          "is_secured":false, 
          "nis_domains":null, 
          "storage_platform_resource_key":"e1ee32f9-6576-11e7-82a8-00a098697714", 
          "name":"vs1", 
          "nis_servers":null, 
          "created_timestamp":1501208944094, 
          "dns_domains":null, 
          "key":"f59dacca-7379-11e7-82a8-00a098697714" 
         }] 
      } 
} 

ここでは「キー」フィールドを抽出します。以下の構文を試してみましたが、私が欲しいものを得ることができませんでした。

var dat map[string]interface{} 

// Unmarshall the JSON body 
if err := json.Unmarshal(body, &dat); err != nil { 
    fmt.Println(err) 
} 
svmRecordsMap := dat["result"].(map[string]interface{})["records"] 
fmt.Printf("%+v", svmRecordsMap) 

result := (svmRecordsMap["key"].([]interface{})[0]).(map[string]interface{}) 

ここのヘルプは非常に高く評価されます。私は対応する構造体の定義とコピーの作成を検討していないことに注意してください。

ありがとうございます!

+1

をsvmRecordsMapするためにアクセスすると、「[キー]を」削除する必要がありますが正常に何を私を得ることができない」に展開することができます欲しいです"?あなたが持っている特定の問題は何ですか? – Adrian

+0

この質問/回答をご覧ください:https://stackoverflow.com/questions/20154606/marshall-and-unmarshall-json-content-in-golang?rq=1 –

+0

[Marshall and UnMarshall JSON Content in GoLang](https://stackoverflow.com/questions/20154606/marshall-and-unmarshall-json-content-in-golang) –

答えて

0

私はこのsolutionは、追加のtypeを作成することなく、このJSONからkeyを抽出するために何をするべきかを示唆していると思います。

+0

ありがとう!しかし、もし私がちょうど "レコード"の下の最初のレコードの "キー"の価値をしたいのですか? – Rakshith

+0

@Rakshith 'for _、l3Elem:= range l3 {...}の代わりに を実行する代わりに' var values map [string] interface {}; エラー:= json.Unmarshal(l3 [0]、&values) ' –

0

私はあなたに例をみましょう:https://play.golang.org/p/tReMMtGA2V

package main 

import (
    "encoding/json" 
    "fmt" 
) 

func main() { 

    txt := `{ 
"status":{"code":"SUCCESS"}, 
"result": { 
       "total_records":1, 
       "records": [{ 
          "last_modified_timestamp":1501209015807, 
          "dns_servers":null, 
          "is_secured":false, 
          "nis_domains":null, 
          "storage_platform_resource_key":"e1ee32f9-6576-11e7-82a8-00a098697714", 
          "name":"vs1", 
          "nis_servers":null, 
          "created_timestamp":1501208944094, 
          "dns_domains":null, 
          "key":"f59dacca-7379-11e7-82a8-00a098697714" 
         }] 
      } 
}` 

    var dat map[string]interface{} 

    if err := json.Unmarshal([]byte(txt), &dat); err != nil { 
     fmt.Println(err) 
    } 
    svmRecordsMap := dat["result"].(map[string]interface{})["records"] 
    fmt.Printf("%+v", svmRecordsMap) 

    result := (svmRecordsMap.([]interface{})[0]).(map[string]interface{}) 

    fmt.Println(result) 

} 

あなたの例では、あなたが

関連する問題