Home > other >  How recursive parse the json string string
How recursive parse the json string string

Time:11-03

How should if the following json string string, recursive parsing json string string

{" Name ":" Wednesday ", "Age" : 6, the fact they often providerent-free: [" Gomez, "" Moticia]", "data" : [{" a ":" aa ", "b", "bb"}, {" c ":" cc "}, {" a list ": [" dd", 1, "ff"]}]}

CodePudding user response:

Self-reliance, basic can parse,

Func jsonParse (f interface {}) {
M:=f (map [string] interface {})
For k, v: range m={
The switch vv:=v (type) {
Case string:
FMT. Println (k, "[string] :", vv)
Case float64:
FMT. Println (k, "[float64] :", vv)
Case bool:
FMT. Println (k, "[bool] :", vv)
Case nil:
FMT. Println (k, "(nil) :", vv)
Case interface [] {} :
FMT. Println (k, "[array] :")
For I, u: range vv={
//var f1 interface {}
The switch vv1:=u. (type) {
Case string:
FMT. Println (I, "[string_] :", u)
Case float64:
FMT. Println (I, "[float64_] :", u)
Case bool:
FMT. Println (I, "[bool_] :", u)
Case nil:
FMT. Println (I, "[nil_] :", u)
Case interface [] {} :
FMT. Println (I, "[array_] :", u)
//m1:=u. (map [string] interface {})
//jsonParse (u)
Case interface {} :
FMT. Println (I, "[interface_] :", u)
M1:=u. (map [string] interface {})
JsonParse (m1)
Default:
FMT. Println (", "I," [type? _], "u,",, "vv1)
}
}
Case interface {} :
FMT. Println (k, "[interface] :", vv)
M1:=v (map [string] interface {})
JsonParse (m1)
Default:
FMT. Println (k, "/type? ", vv)
}
}
}
Func main () {

B:=byte [] (` {" Name ":" Wednesday ", "Age" : 6, the fact they often providerent-free: [" Gomez, "" Moticia]", "data" : [{" a ":" aa ", "b" : "bb"}, {" c ":" cc "}, {" a list ": [" dd", 1, "ff"]}]} `)
Var f interface {}
Err:=json. The Unmarshal (b, & amp; F)
if err ! Nil={
FMT. Println (err)
}
JsonParse (f)
}

CodePudding user response:

 
Package the main

Import (
"FMT"
"Encoding/json"
)
Func jsonArrayParse (vv interface [] {}) {
For I, u: range vv={
//var f1 interface {}
The switch vv1:=u. (type) {
Case string:
FMT. Println (I, "[string_] :", u)
Case float64:
FMT. Println (I, "[float64_] :", u)
Case bool:
FMT. Println (I, "[bool_] :", u)
Case nil:
FMT. Println (I, "[nil_] :", u)
Case interface [] {} :
FMT. Println (I, "[array_] :", u)
JsonArrayParse (vv1)
Case interface {} :
FMT. Println (I, "[interface_] :", u)
M1:=u. (map [string] interface {})
JsonObjectParse (m1)
Default:
FMT. Println (", "I," [type? _], "u,",, "vv1)
}
}
}

Func jsonObjectParse (f interface {}) {
M:=f (map [string] interface {})
For k, v: range m={
The switch vv:=v (type) {
Case string:
FMT. Println (k, "[string] :", vv)
Case float64:
FMT. Println (k, "[float64] :", vv)
Case bool:
FMT. Println (k, "[bool] :", vv)
Case nil:
FMT. Println (k, "(nil) :", vv)
Case interface [] {} :
FMT. Println (k, "[array] :")
JsonArrayParse (vv)
Case interface {} :
FMT. Println (k, "[interface] :", vv)
M1:=v (map [string] interface {})
JsonObjectParse (m1)
Default:
FMT. Println (k, "/type? ", vv)
}
}
}
Func main () {
JsonStr:=byte [] (` {" Name ":" aree ", "Age" : 18, "From" : [" SZ ", "GD"], "data" : [{" a ":" aa ", "b" : null}, {" c ": []}, {" a list" : [" dd ", 1, "650827..."]}]} `)
//jsonStr:=byte [] (` [{" Name ":" aree ", "Age" : 18, "From" : [" SZ ", "GD"], "data" : [{" a ":" aa ", "b" : null}, {" c ": []}, {" a list" : [" dd ", 1, "ff"]}]}] `)

If (strings. The Index (string (jsonStr [:]), "[")==0) {
Var f interface [] {}
Err:=json. The Unmarshal (jsonStr, & amp; F)
if err ! Nil={
FMT. Println (err)
}
JsonArrayParse (f)
} else {
Var f interface {}
Err:=json. The Unmarshal (jsonStr, & amp; F)
if err ! Nil={
FMT. Println (err)
}
JsonObjectParse (f)
}
}

CodePudding user response:

Json format is fixed? If so, can use the tag structure + json, and then use the "encoding/json" Unmarshal method

CodePudding user response:

No matter how complex the json, can be decomposed into the map and sliced array

Var interface p {}
Err=json. The Unmarshal (data, & amp; P)
.

CodePudding user response:

Gjson know about the

CodePudding user response:

I also want to ask, if the level of deep Json, and no laws, it is defined in advance at each level structure, that is too much trouble

CodePudding user response:

So generally defined beforehand good schema, with the tools such as schematyper automatically generate a json structure

CodePudding user response:

Jsonparser

This level is very suitable for deep parsing json
  • Related