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 Android SDK

Describes the OptimizelyJSON object, which the Optimizely Feature Experimentation Android SDK uses to retrieve JSON.

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

Version

SDK v3.6 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,Object>)

getValue

String jsonKey, Class<T> clazz

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 keynestedField in{field1: {nestedField: "blah"}}, you can callgetValue with the parameter"field1.nestedField2".

TheOptimizelyJSON object is defined as follows:

public class OptimizelyJSON {    public String toString();      public Map<String,Object> toMap();      public <T> T getValue(@Nullable String jsonKey, Class<T> clazz) throws JsonParseException}

Examples

You can easily use the OptimizelyJSON 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 a schema object into which you want to unmarshal OptimizelyJson content:data class SSub(var field: String)data class SObj(   var field1: Int,   var field2: Double,   var field3: String,   var field4: SSub)try {   //parse all json key/value pairs into your schema, sObj   val robj = optlyJSON.getValue(null, SObj::class.java)   //or, parse the specified key/value pair with an integer value   val rint = optlyJSON.getValue("field1", Int::class.java)   //or, parse the specified key/value pair with a string value   val rstr = optlyJSON.getValue("field4.field", String::class.java)} catch (e: JsonParseException) {   e.printStackTrace()}
//declare a schema object into which you want to unmarshal OptimizelyJson content:class SSub {   String field;}class SObj {   int field1;   double field2;   String field3;   SSub field4;}try {   //parse all json key/value pairs into your schema, sObj   SObj robj = optlyJSON.getValue(null, SObj.class);   //or, parse the specified key/value pair with an integer value   Integer rint = optlyJSON.getValue("field1", Integer.class);   //or, parse the specified key/value pair with a string value   String rstr = optlyJSON.getValue("field4.field", String.class);} catch (JsonParseException e) {   e.printStackTrace();}

Updated 17 days ago



[8]ページ先頭

©2009-2025 Movatter.jp