云眼JSON
November 5, 2023About 1 min
云眼JSON
本主题介绍 EyeofcloudJSON 对象,Eyeofcloud Feature Experimentation Java SDK 使用该对象检索 JSON。
由于静态类型语言缺乏对 JSON 的原生支持,Java SDK 使用 EyeofcloudJSON 对象以灵活的方式检索 JSON。
版本
SDK v3.5 及更高版本
方法
可以使用以下方法访问 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" |
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 对象来填充您声明的架构对象。
Java
//declare a schema object into which you want to unmarshal EyeofcloudJson 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 = 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
var strValue string
SSub rsub = eocJSON.getValue("field4.field", SSub.class)