Movatterモバイル変換


[0]ホーム

URL:


Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog InFeature Experimentation
Dev guide
All
Pages
Start typing to search…

OptimizelyJSON for the Go SDK

Describes the OptimizelyJSON object that the Optimizely Feature Experimentation Go SDK uses to retrieve JSON.

Since statically typed languages lack native support for JSON, the Go SDK uses the OptimizelyJSON object to retrieve JSON in a flexible way.

Version

Go 1.3 and higher

Methods

You can access JSON representations with the following methods:

Method

Parameters

Description

ToString

none

Returns a string representation of the JSON object

ToMap

none

Returns a map representation of the JSON object:(map[string]interface{})

GetValue

jsonKey string, sObj interface{}

Populates a specified schema object (sObj) with the key/value pair of the JSON key you pass to this method.

If JSON key is empty, it populates your schema object with all JSON key/value pairs.

You can retrieve information for a nested member of the JSON data structure by using flattened JSON dot notation.
For example, if you want to access the keynestedField2 in{field1: {nestedField2: "blah"}}, you can callGetValue with the parameter"field1.nestedField2".

The OptimizelyJSON object is defined as follows:

// OptimizelyJSON is an object for accessing JSON type OptimizelyJSON struct {  ToString() string   //The underlying variable for the OptimizelyJson object is `map[string]interface{}`, which is the return value for `ToMap` method.  ToMap() map[string]interface{}   //"sObj" holds the schema that the user needs to pass by reference to unmarshal json content into it  GetValue(jsonKey string, sObj interface{}) error}

Examples

You can easily useOptimizelyJSON object, for example to:

  • Get a JSON string by calling theToString method, or
  • Retrieve a specified schema from theOptimizelyJSON object by calling theGetValue method.

The following example shows how to use an OptimizelyJSON object to populate a schema object you declare.

//declare "sObj" to hold the schema object into which you later unmarshal OptimizelyJson content:type schemaObj struct {Field1 intField2 float64Field3 stringField4 struct {Field string} }sObj := schemaObj{}//not shown: get an optimizelyJSON object, optlyJSON//parse all json key/value pairs into your schema, sObjerr := optlyJSON.GetValue("", &sObj)  //or, parse the specified key/value pair with an integer value var intValue interr := optlyJSON.GetValue("field1", &intValue) // intValue stores integer value//or, parse the specified key/value pair with a string valuevar strValue stringerr := optlyJSON.GetValue("field4.field", &strValue) // strValue stores string value

Updated 17 days ago



[8]ページ先頭

©2009-2025 Movatter.jp