Golang Gin converts json into an array to achieve JSON rendering output

mysql

The following is the json array queried in the databaseļ¼ŒuserAudit.MainImage
[{"status":"2","url":"https://cro.com/feedback/20221214154543820.jpg?x-oss-process=image/resize,p_13"},{"status":"0","url":"https://cro.com/feedback/20221214154545079.jpg?x-oss-process=image/resize,p_13"},{"status":"0","url":"https://cro.com/feedback/20221214154546158.jpg?x-oss-process=image/resize,p_13"},{"status":"2","url":"https:/cro.com/feedback/20221214154547151.jpg?x-oss-process=image/resize,p_13"}]

func AuditMainImage(userId int) []string {
    var userAudit models.MuseSmallUserAudit
    models.Db.Where("user_id = ?", userId).First(&userAudit)
    type AuditMainImage []struct {
        Status string `json:"status"`
        Url    string `json:"url"`
    }
    var mainImage AuditMainImage
    _ = json.Unmarshal([]byte(userAudit.MainImage), &mainImage) 
    newMap := []string{}
    for _, eachItem := range mainImage {
        if eachItem.Status == "2" {
            newMap = append(newMap, eachItem.Url)
        }
    }
    return newMap
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish