2 回答
TA贡献1818条经验 获得超8个赞
我终于成功地获取了键/值,并且现在可以同时替换任何值或键,但我现在正在努力的最后一步是如何分别获取所有值的aMap[key]
和路径。anArray[i]
例如 "test2": "TEST EACH REPLACE "
有一个路径outterJSON.innerJSON1.nnerInnerArray.1.test2
但可以是任何格式。
https://play.golang.org/p/TNkonE8AkoJ
package main
import (
"encoding/json"
"fmt"
)
var co=""
func main() {
//Creating the maps for JSON
m := map[string]interface{}{}
//Parsing/Unmarshalling JSON encoding/json
err := json.Unmarshal([]byte(input), &m)
if err != nil {
panic(err)
}
parseMap(m)
fmt.Println(co)
data, _ := json.MarshalIndent(m,"", " ")
fmt.Println(string(data))
}
func parseMap(aMap map[string]interface{}) {
for key, val := range aMap {
switch concreteVal := val.(type) {
case map[string]interface{}:
co += key+"."
fmt.Println(key+"NwHN")
parseMap(val.(map[string]interface{}))
case []interface{}:
co += key+"."
fmt.Println(key+"NHN")
parseArray(val.([]interface{}))
default:
fmt.Println("OOOOO")
aMap[key]="TEST EACH REPLACE "
fmt.Println(key, "v:", concreteVal)
}
}
}
func parseArray(anArray []interface{}) {
for i, val := range anArray {
switch concreteVal := val.(type) {
case map[string]interface{}:
fmt.Println("Indexr:", i)
parseMap(val.(map[string]interface{}))
case []interface{}:
fmt.Println("Indexb:", i)
parseArray(val.([]interface{}))
default:
anArray[i]= "TEST EACH REPLACE ARR"
fmt.Println("Index", i, ":", concreteVal)
}
}
}
const input = `
{
"ouj":"ou",
"outterJSON":{
"innerJSON1":{
"value1":10,
"value2":22
,
"InnerInnerArray": [ "test1" , {"test2":"valtest2"}],
"InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]
},
"InnerJSON2":"NoneValue"
}
}
`
TA贡献1891条经验 获得超3个赞
最后!我完成了我要找的东西的完整规格!!
https://play.golang.org/p/eN4-FjaQS97
package main
import (
"encoding/json"
"fmt"
)
func main() {
b := []byte(`
{
"iw":{"Ie":{"Itye":{"e":"eIe"}}},
"InnerJSON2":"NoneValue",
"outterJSON":{
"innerJSON1":{
"value1":10,
"value2":22
,
"InnerInnerArray": [ "test1" , "test2"],
"InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]
},
"InnerJSON2":"NoneValue"
}
}
`)
f := map[string]interface{}{}
if err := json.Unmarshal(b, &f); err != nil {
panic(err)
}
verifyJSON(f)
data, _ := json.MarshalIndent(f, "", " ")
fmt.Println(string(data))
}
func verifyJSON(bv interface{}) {
var dumpJSON func(v interface{}, kn string)
dumpJSON = func(v interface{}, kn string) {
iterMap := func(x map[string]interface{}, root string) {
var knf string
if root == "root" {
knf = "%v/%v"
} else {
knf = "%v/%v"
}
for k, v := range x {
switch vv := v.(type) {
case map[string]interface{}:
fmt.Printf("%s => (map[string]interface{}) ...\n", fmt.Sprintf(knf, root, k))
case []interface{}:
fmt.Printf("%s => ([]interface{}) ...\n", fmt.Sprintf(knf, root, k))
default:
fmt.Printf("%s => %v\n", fmt.Sprintf(knf, root, k), vv)
x[k] = "rgk"
}
dumpJSON(v, fmt.Sprintf(knf, root, k))
}
}
iterSlice := func(x []interface{}, root string) {
var knf string
if root == "root" {
knf = "%v/%v"
} else {
knf = "%v/%v"
}
for k, v := range x {
switch vv := v.(type) {
case map[string]interface{}:
fmt.Printf("%s => (map[string]interface{}) ...\n", fmt.Sprintf(knf, root, k))
case []interface{}:
fmt.Printf("%s => ([]interface{}) ...\n", fmt.Sprintf(knf, root, k))
default:
fmt.Printf("%s => %v\n", fmt.Sprintf(knf, root, k), vv)
x[k] = "rg"
}
dumpJSON(v, fmt.Sprintf(knf, root, k))
}
}
switch vv := v.(type) {
case map[string]interface{}:
iterMap(vv, kn)
case []interface{}:
iterSlice(vv, kn)
default:
}
}
dumpJSON(bv, "root")
}
- 2 回答
- 0 关注
- 212 浏览
添加回答
举报