Created
August 3, 2011 03:54
-
-
Save bemasher/1121873 to your computer and use it in GitHub Desktop.
Parsing JSON received from an http request in go.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"fmt" | |
"http" | |
"json" | |
"strings" | |
) | |
const ( | |
MaxCon = 4 | |
Query = `{ | |
"SubCategoryId": 65, | |
"NValue": "", | |
"StoreDepaId": 1, | |
"NodeId": 8652, | |
"BrandId": -1, | |
"PageNumber": %d, | |
"CategoryId": 234 | |
}` | |
SearchURL = "http://www.ows.newegg.com/Search.egg/Advanced" | |
) | |
type JSONResponse struct { | |
SubCategoryId int | |
HasHasSimilarItems bool | |
NavigationContentList []interface{} | |
CanBeCompare bool | |
CoremetricsInfo interface{} | |
PaginationInfo PaginationInfo | |
MasterComboStoreId int | |
SearchProvider int | |
IsAllComboBundle bool | |
RelatedLinkList interface{} | |
ProductListItems []interface{} | |
SearchResultType int | |
HasDeactivatedItems bool | |
} | |
type PaginationInfo struct { | |
PageCount int | |
TotalCount int | |
PageNumber int | |
Pagesize int | |
} | |
func init() { | |
log.SetFlags(log.Lshortfile | log.Ltime) | |
} | |
func main() { | |
r, err := http.Post(SearchURL, "application/x-www-form-urlencoded", strings.NewReader(fmt.Sprintf(Query, 1))) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
decoder := json.NewDecoder(r.Body) | |
var data JSONResponse | |
defer r.Body.Close() | |
err = decoder.Decode(&data) | |
if err != nil { | |
log.Println(err) | |
return | |
} | |
log.Println(data.PaginationInfo) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"SubCategoryId": 65, | |
"HasHasSimilarItems": false, | |
"NavigationContentList": [ | |
{ | |
"NavigationItemList": [ | |
{ | |
"SubCategoryId": -1, | |
"Description": "Discount Item", | |
"StoreDepaId": -1, | |
"NValue": "100008652 4803", | |
"BrandId": -1, | |
"StoreType": -1, | |
"ItemCount": 232, | |
"CategoryId": -1, | |
"ElementValue": "4803" | |
}, | |
... | |
"PaginationInfo": { | |
"TotalCount": 650, | |
"PageNumber": 1, | |
"PageSize": 20, | |
"PageCount": 0 | |
}, | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment