云眼JSON
May 11, 2023About 2 min
云眼JSON
本主题介绍 EyeofcloudJSON 对象,Eyeofcloud Feature Experimentation Android SDK 使用该对象检索 JSON。
由于静态类型语言缺乏对 JSON 的原生支持,Android SDK 使用 EyeofcloudJSON 对象以灵活的方式检索 JSON。
版本
SDK v3.6 及更高版本
方法
可以使用以下方法访问 JSON 表示形式:
方法 | 参数 | 描述 |
---|---|---|
toString | 没有 | 返回 JSON 对象的字符串表示形式 |
toMap | 没有 | 返回 JSON 对象的映射表示形式:(Map<String,Object>) |
getValue | String jsonKey, Class<T> clazz | 返回指定的架构对象 (T ),其中包含传递给此方法的 JSON 键的键/值对。 如果 JSON 键为 null 或为空,则会使用所有 JSON 键/值对填充架构对象。 可以使用平展的 JSON 点表示法检索 JSON 数据结构的嵌套成员的信息。 例如,如果要访问{field1: {nestedField: "blah"}} 中的键nestedField ,可以使用"field1.nestedField2" 参数进行调用getValue 。 |
EyeofcloudJSON
对象定义如下:
Java
public class EyeofcloudJSON {
public String toString();
public Map<String,Object> toMap();
public <T> T getValue(@Nullable String jsonKey, Class<T> clazz) throws JsonParseException
}
例子
可以轻松使用 EyeofcloudJSON 对象,例如:
- 通过调用
toString
方法获取 JSON 字符串,或者 - 通过调用
getValue
方法从EyeofcloudJSON
对象中检索指定的架构。
以下示例演示如何使用 EyeofcloudJSON 对象来填充您声明的架构对象。
Kotlin
//declare a schema object into which you want to unmarshal EyeofcloudJson 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 = eocJSON.getValue(null, SObj::class.java)
//or, parse the specified key/value pair with an integer value
val rint = eocJSON.getValue("field1", Int::class.java)
//or, parse the specified key/value pair with a string value
val rstr = eocJSON.getValue("field4.field", String::class.java)
} catch (e: JsonParseException) {
e.printStackTrace()
}
Java
//declare a schema object into which you want to unmarshal EyeofcloudJson 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 = eocJSON.getValue(null, SObj.class);
//or, parse the specified key/value pair with an integer value
Integer rint = eocJSON.getValue("field1", Integer.class);
//or, parse the specified key/value pair with a string value
String rstr = eocJSON.getValue("field4.field", String.class);
} catch (JsonParseException e) {
e.printStackTrace();
}