Dev guideAPI Reference
Dev guideAPI ReferenceUser GuideGitHubNuGetDev CommunityDoc feedbackLog In

OptimizelyJSON

This topic describes the OptimizelyJSON object that the Optimizely Feature Experimentation C# SDK uses to retrieve JSON.

Since static-typed languages lack native support for JSON, the C# SDK uses the OptimizelyJson object to retrieve JSON in a flexible way.

Version

SDK v3.5 and higher

Methods

You can access JSON representations with the following methods:

MethodParametersDescription
ToStringnoneReturns a string representation of the JSON object
ToDictionarynoneReturns a dictionary representation of the JSON object: (Dictionary<string,object>)
GetValue<T>string jsonPathReturns 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");