/* Here is an example of the JSON I'm trying to parse [It's from the Guardian website]:{"response":{"status":"ok","userTier":"developer","total":19,"startIndex":1,"pageSize":1,"currentPage":1,"pages":19,"orderBy":"relevance","results":[{"type":"article","sectionId":"technology","webTitle":"Apple removes adblockers that work on Facebook and other third-party apps","webPublicationDate":"2015-10-09T10:02:09Z","id":"technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","webUrl":"http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","apiUrl":"http://content.guardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps","sectionName":"Technology"}]}}I want to get "webTitle" which is nested inside "results" which is nested inside "response"*/// get the JSON from URLresponse,err:=//http.Get("http://content.guardianapis.com/search?from-date=2015-10-03&q=iphone&api-key=zksex4jr82dg2gkcqtyq5xp8")iferr!=nil{fmt.Printf("%s",err)os.Exit(1)}else{deferresponse.Body.Close()contents,err:=ioutil.ReadAll(response.Body)iferr!=nil{fmt.Printf("%s",err)os.Exit(1)}//unmarshal JSON into "parse1"varparse1map[string]interface{}iferr:=json.Unmarshal(contents,&parse1);err!=nil{panic(err)}fmt.Println("||||||PARSE1: ",(reflect.TypeOf(parse1))," ||||||\n",parse1,"|||||||| END PARSE1")/* Produces:||||||PARSE1 which is map[string]interface {} |||||| map[response:map[status:ok userTier:developer total:19 startIndex:1 currentPage:1 pages:19 orderBy:relevance pageSize:1 results:[map[webTitle:Apple removes adblockers that work on Facebook and other third-party apps webPublicationDate:2015-10-09T10:02:09Z id:technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps webUrl:http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps apiUrl:http://content.guardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-appssectionName:Technology type:article sectionId:technology]]]] |||||||| END PARSE1OK. Now lets get the "response" map:*/parse2:=parse1["response"]fmt.Println("||||||PARSE2: ",(reflect.TypeOf(parse2))," ||||||\n",parse2,"|||||||| END PARSE2")/* Produces:||||||PARSE2 which is map[string]interface {} |||||| map[pageSize:1 results:[map[sectionName:Technology type:article sectionId:technology webTitle:Apple removes adblockers that work on Facebook and other third-party apps webPublicationDate:2015-10-09T10:02:09Z id:technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps webUrl:http://www.theguardian.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps apiUrl:http://content.guardianapis.com/technology/2015/oct/09/apple-removes-iphone-adblockers-facebook-third-party-apps]] total:19 startIndex:1 currentPage:1 pages:19 orderBy:relevance status:ok userTier:developer] |||||||| END PARSE2Now get the "results" map:*/parse3:=parse2["results"]fmt.Println("||||||PARSE3: ",(reflect.TypeOf(parse3))," ||||||\n",parse3,"|||||||| END PARSE3")/* Produces:invalid operation: parse2["results"] (type interface {} does not support indexing)So, why was does: 'parse2 := parse1["response"]' work but 'parse3 := parse2["results"]' doesn't?[All assuming I'm even going aobut trying to do this the right way in the first place!]*/
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.