拆分后如何将数组转换为嵌套的json对象

拆分后如何将数组转换为嵌套的json对象

问题描述:

I'm trying to deal with some error descriptions from this library because I need them to be nested JSON objects.

The errors seem to be an array originally, like this:

["String length must be greater than or equal to 3","Does not match format 'email'"]

I needed that to also include the field name of the containing error:

["Field1: String length must be greater than or equal to 3","Email1: Does not match format 'email'"]

After that I need to split each array value by colon : so I can have the field name and error description in separate variables like slice[0] and slice[1].

With that I want to make a nested JSON object like so:

{
    "errors": {
        "Field1": "String length must be greater than or equal to 3",
        "Email1": "Does not match format 'email'"
    }
}

This is my way of trying to achieve this:

var errors []string
for _, err := range result.Errors() {
    // Append the errors into an array that we can use to split later
    errors = append(errors, err.Field() + ":" + err.Description())
}

// Make the JSON map we want to append values to
resultMap := map[string]interface{}{
    "errors": map[string]string {
        "Field1": "",
        "Email1": ""
    },
}

// So we actually can use the index keys when appending
resultMapErrors, _ := resultMap["errors"].(map[string]string)

for _, split := range errors {
    slice := strings.Split(split, ":")
    for _, appendToMap := range resultMapErrors {
        appendToMap[slice[0]] = slice[1] // append it like so?
    }
}

finalErrors, _ := json.Marshal(resultMapErrors)
fmt.Println(string(finalErrors))

But this throws the errors

main.go:59:28: non-integer string index slice[0]
main.go:59:39: cannot assign to appendToMap[slice[0]]

Any clue how I can achieve this?

我正在尝试处理此库,因为我需要将它们嵌套在JSON对象中。 p>

错误最初似乎是一个数组,如下所示: p>

  [“字符串长度必须大于或等于3”,“与格式'email'不匹配”] 
  code>  pre> 
  
 

我还需要包含错误的字段名称: p>

  [“ Field1:字符串长度必须大于或等于3”,“  Email1:与格式'email'“]不匹配
  code>  pre> 
 
 

之后,我需要用冒号: code>分割每个数组值,以便 在单独的变量(例如 slice [0] code>和 slice [1] code>)中具有字段名称和错误说明。 p>

为此,我想制作一个嵌套的JSON对象,如下所示: p>

  {
“ errors”:{
“ Field1  “:”“字符串长度必须大于或等于3”,
“ Email1”:“与格式'email'不匹配” 
} 
} 
  code>  pre> 
 
  

这是我尝试实现的方法: p>

  var errors [] string 
for _,err:= range result.Errors(){
 // 将错误附加到一个数组中,以供以后拆分使用
错误= append(errors,err.Field()+“:” + err.Description())
} 
 
 //创建JSON映射 我们想将值附加到
resultMap:= map [string] interface {} {
“ errors”:map [string] string {
“ Field1”:“”,
“ Email1”:“” 
}  ,
} 
 
 //因此,我们实际上可以在附加
resultMapErrors时使用索引键,_:= resultMap [“ errors”]。(map [string] string)
 
for _,split:= range 错误{
 slice:= strings.Split(split(“:”))
 _,appendToMap:=范围resultMapErrors {
 appendToMap [slice [0]] = slice [1] //将其追加 像这样吗?
} 
} 
 
finalErrors,_:= json.Marshal(resultMapErrors)
fmt.Println(string(finalErrors))
  code>  pre> 
 
 

但这会引发错误 p>

  main.go:59:28:非整数字符串索引slice [0] 
main.go:59:39:无法分配给appendToMap [  slice [0]] 
  code>  pre> 
 
 

有什么线索可以实现吗? p> div>

var errors = make(map[string]string)
for _, err := range result.Errors() {
    errors[err.Field()] = err.Description()
}

// Make the JSON map we want to append values to
resultMap := map[string]interface{}{
    "errors": errors,
}

finalErrors, _ := json.Marshal(resultMap)
fmt.Println(string(finalErrors))