云眼JSON

云眼About 2 min

云眼JSON

本主题介绍云眼灰度发布(特性标帜)AB实验 Go SDK 用于检索 JSON 的 EyeofcloudJSON 对象。

由于静态类型语言缺乏对 JSON 的原生支持,Go SDK 使用 EyeofcloudJSON 对象以灵活的方式检索 JSON。

版本

Go 1.3 及更高

方法

可以使用以下方法访问 JSON 表示形式:

方法

参数

描述

ToString

没有

返回 JSON 对象的字符串表示形式

ToMap

没有

返回 JSON 对象的映射表示形式:(map[string]interface{})

GetValue

jsonKey string, sObj interface{}

使用传递给此方法的 JSON 键的键/值对填充指定的架构对象 ()。

如果 JSON 键为空,则会使用所有 JSON 键/值对填充架构对象。

可以使用平展的 JSON 点表示法检索 JSON 数据结构的嵌套成员的信息。
例如,如果要访问 中的键,可以使用参数 进行调用。 sObj``nestedField2``{field1: {nestedField2: "blah"}}``GetValue``"field1.nestedField2"

EyeofcloudJSON 对象定义如下:

Go

// EyeofcloudJSON is an object for accessing JSON type EyeofcloudJSON struct { ToString() string //The underlying variable for the EyeofcloudJson object is `map[string]interface{}`, which is the return value for `ToMap` method. ToMap() map[string]interface{} //"sObj" holds the schema that the user needs to pass by reference to unmarshal json content into it GetValue(jsonKey string, sObj interface{}) error }

例子

例如,可以轻松地使用对象来:EyeofcloudJSON

  • 通过调用该方法获取 JSON 字符串,或者 ToString
  • 通过调用该方法从对象中检索指定的架构。 EyeofcloudJSON``GetValue

以下示例演示如何使用 EyeofcloudJSON 对象来填充您声明的架构对象。

Go

//declare "sObj" to hold the schema object into which you later unmarshal EyeofcloudJson content: type schemaObj struct { Field1 int Field2 float64 Field3 string Field4 struct {Field string} } sObj := schemaObj{} //not shown: get an eyeofcloudJSON object, eocJSON //parse all json key/value pairs into your schema, sObj err := eocJSON.GetValue("", &sObj) //or, parse the specified key/value pair with an integer value var intValue int err := eocJSON.GetValue("field1", &intValue) // intValue stores integer value //or, parse the specified key/value pair with a string value var strValue string err := eocJSON.GetValue("field4.field", &strValue) // strValue stores string value

Last update:
Contributors: “zhangweixue”,zhangweixue