检查字符串是否是JSON格式 [英] Check if strings are JSON format
问题描述
如何检查给定的字符串是否以空格/换行符分隔的多个json字符串形式?
例如,
给出: test123 {Name:mike} (3 json连接空格)
return: true
,因为每个项目(test
123
和 { Name:mike}
)是一个有效的json。
在Go中,我可以写一个O(N ^ 2)函数如:
//检查给定的字符串是json还是多个json与空格连接/ newline
func validateJSON(str string )错误{
//只有一个json字符串
if isSON(str){
return nil
}
//多个json字符串与空格连接
str = strings.TrimSpace(str)
arr:= [] rune(str)
start:= 0
end:= 0
for start< len(str){
for end< len(str)&& !unicode.IsSpace(arr [end]){
end ++
}
substr:= str [start:end]
if isSON(substr){
for end < len(str)&& unicode.IsSpace(arr [end]){
end ++
}
start = end
} else {
if end == len(str){
返回errors.New(解析输入时出错:+ substr)
}
for end< len(str)&& unicode.IsSpace(arr [end]){
end ++
}
}
}
返回零
}
func isJSON(str string)bool {
var js json.RawMessage
return json.Unmarshal([] byte(str),& js)== nil
}
但是这对大输入无效。
有两种选择。从编码的角度来看,最简单的方法就是正常解码JSON字符串。你可以通过解码到一个空的结构来实现这个效率:
package main
importencoding / json
func main(){
input:= [] byte(`{a:b,c:123}`)
var x struct {}
if err:= json.Unmarshal(input,& x); err!= nil {
panic(err)
}
input = [] byte(`{a:b,c:123} xxx` )//这个失败
if err:= json.Unmarshal(input,& x); err!= nil {
panic(err)
}
}
< (游乐场链接)
这种方法有一些潜在的缺点:$ b
$ b
- 它只适用于单个JSON对象。也就是说,一个对象列表(如问题中所要求的)将会失败,不需要额外的逻辑。
- 正如@icza在评论中指出的那样,它只能用于JSON对象,因此裸露数组,数字或字符串将失败。为了适应这些类型,必须使用
interface {}
,这会带来一些严重的性能损失。 - 抛弃必须仍然分配
x
值,并且在工作表中可能至少有一次反射调用,这可能会给某些工作负载带来显着的性能损失。 $ b $由于这些限制,我建议使用第二个选项:遍历整个JSON输入,忽略实际内容。使用标准库 json.Decoder : b
$ b
包主要
导入(
字节
编码/ json
io
)
func main(){
input:= [] byte(`{a:b,c:123}`` )
dec:= json.NewDecoder(bytes.NewReader(input))
for {
_,err:= dec.Token()
if err == io.EOF {
break //输入结束,有效的JSON
}
if err!= nil {
panic(err)//输入无效
}
}
$ b input = [] byte(`{a:b,c:123} xxx`)//这个输入失败
dec = json.NewDecoder(bytes .NewReader(input))
for {
_,err:= dec.Token()
if err == io.EOF {
break //输入结束,有效JSON
}
如果错误!=无{
恐慌(错误)//无效输入
}
}
}
(游乐场链接)
How to check if a given string is in form of multiple json string separated by spaces/newline?
For example,
given: "test" 123 {"Name": "mike"}
(3 json concatenated with space)
return: true
, since each of item ("test"
123
and {"Name": "mike"}
) is a valid json.
In Go, I can write a O(N^2) function like:
// check given string is json or multiple json concatenated with space/newline
func validateJSON(str string) error {
// only one json string
if isJSON(str) {
return nil
}
// multiple json string concatenate with spaces
str = strings.TrimSpace(str)
arr := []rune(str)
start := 0
end := 0
for start < len(str) {
for end < len(str) && !unicode.IsSpace(arr[end]) {
end++
}
substr := str[start:end]
if isJSON(substr) {
for end < len(str) && unicode.IsSpace(arr[end]) {
end++
}
start = end
} else {
if end == len(str) {
return errors.New("error when parsing input: " + substr)
}
for end < len(str) && unicode.IsSpace(arr[end]) {
end++
}
}
}
return nil
}
func isJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
But this won't work for large input.
There are two options. The simplest, from a coding standpoint, is going to be just to decode the JSON string normally. You can make this most efficient by decoding to an empty struct:
package main
import "encoding/json"
func main() {
input := []byte(`{"a":"b", "c": 123}`)
var x struct{}
if err := json.Unmarshal(input, &x); err != nil {
panic(err)
}
input = []byte(`{"a":"b", "c": 123}xxx`) // This one fails
if err := json.Unmarshal(input, &x); err != nil {
panic(err)
}
}
This method has a few potential drawbacks:
- It only works with a single JSON object. That is, a list of objects (as requested in the question) will fail, without additional logic.
- As pointed out by @icza in comments, it only works with JSON objects, so bare arrays, numbers, or strings will fail. To accomodate these types,
interface{}
must be used, which introduces the potential for some serious performance penalties. - The throw-away
x
value must still be allocated, and at least one reflection call is likely under the sheets, which may introduce a noticeable performance penalty for some workloads.
Given these limitations, my recommendation is to use the second option: loop through the entire JSON input, ignoring the actual contents. This is made simple with the standard library json.Decoder:
package main
import (
"bytes"
"encoding/json"
"io"
)
func main() {
input := []byte(`{"a":"b", "c": 123}`)
dec := json.NewDecoder(bytes.NewReader(input))
for {
_, err := dec.Token()
if err == io.EOF {
break // End of input, valid JSON
}
if err != nil {
panic(err) // Invalid input
}
}
input = []byte(`{"a":"b", "c": 123}xxx`) // This input fails
dec = json.NewDecoder(bytes.NewReader(input))
for {
_, err := dec.Token()
if err == io.EOF {
break // End of input, valid JSON
}
if err != nil {
panic(err) // Invalid input
}
}
}
这篇关于检查字符串是否是JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!