Beispiel #1
0
// decodePath shows how to use the DecodePath API call.
func decodePath() {
	type UserType struct {
		UserTypeID   int
		UserTypeName string
	}

	type NumberFormat struct {
		DecimalSeparator  string `jpath:"userContext.preferenceInfo.numberFormat.decimalSeparator"`
		GroupingSeparator string `jpath:"userContext.preferenceInfo.numberFormat.groupingSeparator"`
		GroupPattern      string `jpath:"userContext.preferenceInfo.numberFormat.groupPattern"`
	}

	type User struct {
		Session      string   `jpath:"userContext.cobrandConversationCredentials.sessionToken"`
		CobrandID    int      `jpath:"userContext.cobrandId"`
		UserType     UserType `jpath:"userType"`
		LoginName    string   `jpath:"loginName"`
		NumberFormat          // This can also be a pointer to the struct (*NumberFormat).
	}

	doc := []byte(decodePathDoc)
	var docMap map[string]interface{}
	json.Unmarshal(doc, &docMap)

	var u User
	ms.DecodePath(docMap, &u)

	fmt.Printf("DecodePath : %+v\n", u)
}
Beispiel #2
0
// decodePath shows how to use the DecodePath API call with an array of
// sub documents.
func decodePathArray() {
	type Animal struct {
		Barks string `jpath:"barks"`
	}

	type People struct {
		Name    string   `jpath:"name"` // jpath is relative to the array.
		Age     int      `jpath:"age.birth"`
		Animals []Animal `jpath:"age.animals"`
	}

	type Items struct {
		Peoples []People `jpath:"people"` // Specify the location of the array.
	}

	doc := []byte(decodePathArrayDoc)
	var docMap map[string]interface{}
	json.Unmarshal(doc, &docMap)

	var items Items
	ms.DecodePath(docMap, &items)

	fmt.Printf("DecodePathArray : %+v\n", items)
}