Actually, at the entrance - JSON of the following form:

{ "classifiers" : [ { "classifier_id" : "9a9388x45-nlc-1361", "url" : "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/9a9388x45-nlc-1361", "name" : "data", "language" : "en", "created" : "2016-03-27T03:46:13.965Z" }, { "classifier_id" : "9a9388x45-nlc-1363", "url" : "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/9a9388x45-nlc-1363", "name" : "data", "language" : "en", "created" : "2016-03-27T03:51:12.911Z" }, ... ] } 

I am trying to present the following structures:

 type WatsonClassifier struct { ClassifierID string `json:"classifier_id"` Language string `json:"language"` url string username string password string } type WatsonClassifierList struct { Classifiers []Classifier `json:"classifiers"` } 

But as a result:

 classifierList := WatsonClassifierList{} json.Unmarshal(answer, &classifierList) fmt.Println(classifierList) 

I have this exhaust:

 {[<nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>]} 

Those. instead of classifiers - nil. What am I doing wrong?

  • So after all Unmarshall I have to fill the array with data from JSON, but not? And in JSON there is data (actually, the piece shown above is exhaust fmt.Println(string(answer)) ). And yes - if the problem were this, then where did it get the correct number nil-in (8 elements, as it should be). - Alexander Pozharskii

1 answer 1

However, I did 2 very stupid things :-)

  1. Did not handle the error returned by json.Unmarshal .
  2. Deserialized an array of interfaces, not specific types ( []Classifier instead of []WatsonClassifier ).