HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

OptimizelyJSON

This topic describes the OptimizelyJSON object which the Optimizely C# SDK uses.

Since statically typed languages lack native support for JSON, the C# SDK uses the OptimizelyJson object to retrieve JSON in a flexible way. The Get All Feature Variables and Get Feature Variable Json methods use OptimizelyJson to return feature variables. For more information, see Get All Feature Variables and Get Feature Variable.

Version

SDK v3.5 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

ToDictionary

none

Returns a dictionary representation of the JSON object: (Dictionary<string,object>)

GetValue<T>

string jsonPath

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

If JSON key is null or 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 key nestedField2 in {field1: {nestedField2: "blah"}}, you can call GetValue with the parameter "field1.nestedField2".

The OptimizelyJson object is defined as follows:

public class OptimizelyJson {
    public string ToString();
  
    public Dictionary<string,object> ToDictionary();
  
    public T GetValue<T>(string jsonPath);
}

Examples

You can easily use OptimizelyJson object, for example to:

  • Get a JSON string by calling the ToString method, or
  • Retrieve a specified schema from the OptimizelyJson object by calling the GetValue method.

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

//declare a schema object into which you want to unmarshal OptimizelyJson content:
public static class SSub {
    public string field;
}

public static class SObj {
    public int field1; 
    public double field2;
    public string field3;
    public SSub field4;
}

//parse all json key/value pairs into your schema, sObj
SObj robj = optlyJSON.GetValue<SObj>(null);

//or, parse the specified key/value pair with an integer value 
int rint = optlyJSON.GetValue<int>("field1");

//or, parse the specified key/value pair with a string value
SSub rsub = optlyJSON.GetValue<SSub>("field4.field");

Did this page help you?