Documentation
¶
Overview ¶
leptjson.go - Go语言版JSON库
Index ¶
- func FindObjectIndex(v *Value, key string) int
- func GetArraySize(v *Value) int
- func GetBoolean(v *Value) bool
- func GetNumber(v *Value) float64
- func GetObjectKey(v *Value, index int) string
- func GetObjectSize(v *Value) int
- func GetString(v *Value) string
- func SetBoolean(v *Value, b bool)
- func SetNumber(v *Value, n float64)
- func SetString(v *Value, s string)
- type Member
- type ParseError
- type Value
- type ValueType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindObjectIndex ¶
FindObjectIndex 查找JSON对象中指定键的索引
Types ¶
type ParseError ¶
type ParseError int
ParseError 表示解析错误
const ( PARSE_OK ParseError = iota // 解析成功 PARSE_EXPECT_VALUE // 期望一个值 PARSE_INVALID_VALUE // 无效的值 PARSE_ROOT_NOT_SINGULAR // 根节点不唯一 PARSE_NUMBER_TOO_BIG // 数字太大 PARSE_MISS_QUOTATION_MARK // 缺少引号 PARSE_INVALID_STRING_ESCAPE // 无效的转义序列 PARSE_INVALID_STRING_CHAR // 无效的字符 PARSE_INVALID_UNICODE_HEX // 无效的Unicode十六进制 PARSE_INVALID_UNICODE_SURROGATE // 无效的Unicode代理对 PARSE_MISS_COMMA_OR_SQUARE_BRACKET // 缺少逗号或方括号 PARSE_MISS_KEY // 缺少键 PARSE_MISS_COLON // 缺少冒号 PARSE_MISS_COMMA_OR_CURLY_BRACKET // 缺少逗号或花括号 )
解析错误常量
func Parse ¶
func Parse(v *Value, json string) ParseError
Parse 解析JSON文本
解析步骤: 1. 跳过前导空白字符 2. 解析JSON值 3. 跳过后续空白字符 4. 检查是否还有额外内容(这将导致PARSE_ROOT_NOT_SINGULAR错误)
Example (Array) ¶
示例 - 解析数组
v := Value{}
Parse(&v, "[1,2,3]")
fmt.Println(v.String())
Output: [1,2,3]
Example (Error) ¶
示例 - 错误处理
v := Value{}
err := Parse(&v, "[1,2,")
fmt.Println(err)
Output: 缺少逗号或方括号
Example (Error02) ¶
v := Value{}
err := Parse(&v, "{\"name\":\"John\",\"age\":")
fmt.Println(err)
Output: 期望一个值
Example (False) ¶
示例 - 解析false值
v := Value{}
Parse(&v, "false")
fmt.Println(v.String())
Output: false
Example (NestedArray) ¶
示例 - 解析嵌套数组
v := Value{}
Parse(&v, "[[1,2],[3,4],5]")
fmt.Println(v.String())
Output: [[1,2],[3,4],5]
Example (NestedObject) ¶
示例 - 解析嵌套对象
v := Value{}
Parse(&v, "{\"person\":{\"name\":\"John\",\"age\":30},\"isActive\":true}")
fmt.Println(v.String())
Output: {"person":{"name":"John","age":30},"isActive":true}
Example (Null) ¶
示例 - 解析null值
v := Value{}
Parse(&v, "null")
fmt.Println(v.String())
Output: null
Example (Number) ¶
示例 - 解析数字
v := Value{}
Parse(&v, "123.456")
fmt.Println(v.String())
Output: 123.456
Example (Object) ¶
示例 - 解析对象
v := Value{}
Parse(&v, "{\"name\":\"John\",\"age\":30}")
fmt.Println(v.String())
Output: {"name":"John","age":30}
Example (String) ¶
示例 - 解析字符串
v := Value{}
Parse(&v, "\"Hello, World!\"")
fmt.Println(v.String())
Output: "Hello, World!"
Example (True) ¶
示例 - 解析true值
v := Value{}
Parse(&v, "true")
fmt.Println(v.String())
Output: true
type Value ¶
type Value struct {
Type ValueType `json:"type"` // 值类型
N float64 `json:"n"` // 数字值(当Type为NUMBER时有效)
S string `json:"s"` // 字符串值(当Type为STRING时有效)
A []*Value `json:"a"` // 数组值(当Type为ARRAY时有效)
O []Member `json:"o"` // 对象值(当Type为OBJECT时有效)
}
Value 表示一个JSON值
func GetObjectValueByKey ¶
GetObjectValueByKey 根据键获取JSON对象的值
Click to show internal directories.
Click to hide internal directories.