12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package test
- import (
- "encoding/json"
- "fmt"
- "go.mongodb.org/mongo-driver/bson"
- "reflect"
- "testing"
- )
- func TestZHUANHUAN(t *testing.T) {
- // 禁止参数数组,数组只能为对象方式上传
- articleStrings := `{
- "A": "支付成功",
- "B": {
- "BA": "transaction",
- "BB": {
- "BBA": "1",
- "BBB": "1"
- },
- "BC": [
- {
- "BCA": "1",
- "BCB": "1"
- },
- {
- "BCA": "1",
- "BCB": "1"
- }
- ],
- "BD": "BD1",
- "BEXXX": ["BE1","BE2","BE3"]
- },
- "C": "CC支付成功",
- "d": "DD支付成功",
- "EXXX": ["E1","E2","E3"]
-
- }`
- articleStrings = `{"clientID":"869023065073597","msgID":"24152","params":[{"typeCode":"123","varData":[{"name":"TempSet","value":"27"}]}]}`
- var articleSlide map[string]interface{}
- multiErr := json.Unmarshal([]byte(articleStrings), &articleSlide)
- if multiErr != nil {
- fmt.Println("转换出错:", multiErr)
- }
- fmt.Println(articleSlide)
- AnalysisMap(articleSlide, "")
- }
- func AnalysisMap(ArticleSlide map[string]interface{}, JointTab string) {
- //JointTab += "."
- T_r := make(map[string]interface{})
- for key, value := range ArticleSlide {
- //fmt.Println(reflect.TypeOf(value).String())
- switch reflect.TypeOf(value).String() {
- case "map[string]interface {}":
- AnalysisMap(value.(map[string]interface{}), JointTab+key+"_")
- break
- case "[]interface {}":
- for _, valuex := range value.([]interface{}) {
- if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
- AnalysisMap(valuex.(map[string]interface{}), JointTab+key+"_")
- }
- }
- break
- default:
- T_r[key] = value
- if len(JointTab) == 0 {
- Data_Add("202300000001_"+key, &bson.M{key: value})
- }
- break
- }
- }
- if len(JointTab) == 0 {
- return
- }
- JointTab = JointTab[:len(JointTab)-1]
- fmt.Println(JointTab, "-------")
- bson_r := bson.M{}
- for key, value := range T_r {
- fmt.Println(key, "->:", value)
- bson_r[key] = value
- }
- Data_Add("202300000001_"+JointTab, &bson_r)
- }
|