Si consideri il seguente codice:Looping/iterate sopra il secondo livello nidificato JSON in Go lang
package main
import (
"encoding/json"
"fmt"
"reflect"
)
func main() {
//Creating the maps for JSON
m := map[string]interface{}{}
//Parsing/Unmarshalling JSON encoding/json
err := json.Unmarshal([]byte(input), &m)
fmt.Println("\nReflect type of Parsing/Unmarshalling Error Object:\n",reflect.TypeOf(err))
fmt.Println("\nParsing/Unmarshalling Error Object:\n",err)
if err != nil {
panic(err)
}
fmt.Println("\nParsed JSON is as follows:\n",m)
fmt.Println("\nReflect type of parsed json object:\n", reflect.TypeOf(m))
for firstLvlkey, firstLvlValue := range m {
fmt.Println("First Level Key:", firstLvlkey)
fmt.Println("First Level Key reflect type of :", reflect.TypeOf(firstLvlkey))
fmt.Println("First Level Value:", firstLvlValue)
fmt.Println("First Level Value reflect type of :", reflect.TypeOf(firstLvlValue))
// <===============================>
//Here I want to iterate/loop over innerJSON1, InnerJSON2 then reach to level InnerInnerJSONArray - fld1 and fld2
// <===============================>
}
}
const input = `
{
"outterJSON":{
"innerJSON1":{
"value1":10,
"value2":22
,
"InnerInnerArray": [ "test1" , "test2"],
"InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]
},
"InnerJSON2":"NoneValue"
}
}
`
ho qualche requisito come voglio leggere/ottenere tutta la chiave e il valore in String
tipo per qualche adn trasformazione Non riesco a definire lo struct
perché otterrò l'input JSON dinamico (ad es. InnerInnerArray
come stringa, quindi il ciclo di secondo livello mi fornirà l'indice dell'array ed elaborerò ogni JSON con la chiave fld1
e val1
).
Desidero ripetere tutte le coppie chiave/valore contenute al suo interno, qual è il modo più efficiente di attraversare la mappa?
Nota: I Am Newbie per Go-lang, il tuo suggerimento/miglioramento sulla domanda è anche il benvenuto.