zhuan_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.mongodb.org/mongo-driver/bson"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestZHUANHUAN(t *testing.T) {
  10. // 禁止参数数组,数组只能为对象方式上传
  11. articleStrings := `{
  12. "A": "支付成功",
  13. "B": {
  14. "BA": "transaction",
  15. "BB": {
  16. "BBA": "1",
  17. "BBB": "1"
  18. },
  19. "BC": [
  20. {
  21. "BCA": "1",
  22. "BCB": "1"
  23. },
  24. {
  25. "BCA": "1",
  26. "BCB": "1"
  27. }
  28. ],
  29. "BD": "BD1",
  30. "BEXXX": ["BE1","BE2","BE3"]
  31. },
  32. "C": "CC支付成功",
  33. "d": "DD支付成功",
  34. "EXXX": ["E1","E2","E3"]
  35. }`
  36. articleStrings = `{"clientID":"869023065073597","msgID":"24152","params":[{"typeCode":"123","varData":[{"name":"TempSet","value":"27"}]}]}`
  37. var articleSlide map[string]interface{}
  38. multiErr := json.Unmarshal([]byte(articleStrings), &articleSlide)
  39. if multiErr != nil {
  40. fmt.Println("转换出错:", multiErr)
  41. }
  42. fmt.Println(articleSlide)
  43. AnalysisMap(articleSlide, "")
  44. }
  45. func AnalysisMap(ArticleSlide map[string]interface{}, JointTab string) {
  46. //JointTab += "."
  47. T_r := make(map[string]interface{})
  48. for key, value := range ArticleSlide {
  49. //fmt.Println(reflect.TypeOf(value).String())
  50. switch reflect.TypeOf(value).String() {
  51. case "map[string]interface {}":
  52. AnalysisMap(value.(map[string]interface{}), JointTab+key+"_")
  53. break
  54. case "[]interface {}":
  55. for _, valuex := range value.([]interface{}) {
  56. if reflect.TypeOf(valuex).String() == "map[string]interface {}" {
  57. AnalysisMap(valuex.(map[string]interface{}), JointTab+key+"_")
  58. }
  59. }
  60. break
  61. default:
  62. T_r[key] = value
  63. if len(JointTab) == 0 {
  64. Data_Add("202300000001_"+key, &bson.M{key: value})
  65. }
  66. break
  67. }
  68. }
  69. if len(JointTab) == 0 {
  70. return
  71. }
  72. JointTab = JointTab[:len(JointTab)-1]
  73. fmt.Println(JointTab, "-------")
  74. bson_r := bson.M{}
  75. for key, value := range T_r {
  76. fmt.Println(key, "->:", value)
  77. bson_r[key] = value
  78. }
  79. Data_Add("202300000001_"+JointTab, &bson_r)
  80. }