数组结构体遍历问题
结构体类型是这样的:
从数据库中遍历读取结构体的值并且赋值到该结构体中
linkLitem := &apiModel.LinkInfo{}
for rows.Next() {
var i = 0
link := &apiModel.LinkInfo{[]apiModel.LinkInfoKeyPerson{{"0", "0", "0", "0"}}}
if err := rows.Scan(&link.KeyPerson[i].Name, &link.KeyPerson[i].Phone, &link.KeyPerson[i].Relation, &link.KeyPerson[i].Card); err != nil {
log.WithFields(log.Fields{
"scan error": sqlStmt,
"error des": err.Error(),
}).Error("scan result error")
return nil, err
}
linkLitem.KeyPerson = append(linkLitem.KeyPerson, link.KeyPerson[i])
i++
}
gin框架解析静态文件
r.StaticFS("/zips", http.Dir("downloadZips"))
r.StaticFile("/dictionaryList", "utils/dictionary.json")
在本地存一个json文件 直接调用方法就可以返回里面的json数据
结构体里面嵌套数组 以及对应的json
package handler
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
type DictionaryList struct {
CreditStatus []CreditStatus `json:"creditStatus"`
RelationShip []RelationShip `json:"relationShip"`
Promise []Promise `json:"promise"`
Answer []Answer `json:"answer"`
CallerPhone []CallerPhone `json:"callerPhone"`
OperatorPhone []OperatorPhone `json:"operatorPhone"`
Marriage []Marriage `json:"marriage"`
LoanUse []LoanUse `json:"loanUse"`
ClientType []ClientType `json:"clientType"`
CompanyType []CompanyType `json:"companyType"`
CompanyTypeName []CompanyTypeName `json:"companyTypeName"`
Live []Live `json:"live"`
ClientRelationship []ClientRelationship `json:"clientRelationship"`
}
type CreditStatus struct {
Key string `json:"key"`
Value int `json:"value"`
}
type RelationShip struct {
Key string `json:"key"`
Value int `json:"value"`
}
type Promise struct {
Key string `json:"key"`
Value int `json:"value"`
}
type Answer struct {
Key string `json:"key"`
Value int `json:"value"`
}
type CallerPhone struct {
Key string `json:"key"`
Value int `json:"value"`
}
type OperatorPhone struct {
Key string `json:"key"`
Value int `json:"value"`
}
type Marriage struct {
Key string `json:"key"`
Value int `json:"value"`
}
type LoanUse struct {
Key string `json:"key"`
Value int `json:"value"`
}
type ClientType struct {
Key string `json:"key"`
Value int `json:"value"`
}
type CompanyType struct {
Key string `json:"key"`
Value int `json:"value"`
}
type CompanyTypeName struct {
Key string `json:"key"`
Value int `json:"value"`
}
type Live struct {
Key string `json:"key"`
Value int `json:"value"`
}
type ClientRelationship struct {
Key string `json:"key"`
Value int `json:"value"`
}
func FilerRead(c *gin.Context) {
//文件读取任务是将文件内容读取到内存中。
info, err := ioutil.ReadFile("utils/dictionary.json")
if err != nil {
fmt.Println(err)
}
//fmt.Println(info)
result := string(info)
//返回结果
var dictionary DictionaryList
if err := json.Unmarshal([]byte(result), &dictionary); err == nil {
// fmt.Println(someOne)
} else {
fmt.Println(err)
}
c.JSON(http.StatusOK, dictionary)
}
JSON文件
{
"CreditStatus": [
{
"key": "未查询",
"value": 0
},
{
"key": "查询成功",
"value": 1
},
{
"key": "审核中",
"value": 2
},
{
"key": "系统异常",
"value": 3
},
{
"key": "请重新提交",
"value": 4
}
],
"RelationShip": [
{
"key": "本人",
"value": 0
},
{
"key": "配偶",
"value": 1
},
{
"key": "父母",
"value": 2
},
{
"key": "子女",
"value": 3
},
{
"key": "朋友",
"value": 4
},
{
"key": "同事",
"value": 5
},
{
"key": "其他",
"value": 6
}
],
"Answer": [
{
"key": "机主接听",
"value": 0
},
{
"key": "无人接听",
"value": 1
},
{
"key": "拒绝接听",
"value": 2
},
{
"key": "占线",
"value": 3
},
{
"key": "关机",
"value": 4
},
{
"key": "停机",
"value": 5
},
{
"key": "空号",
"value": 6
},
{
"key": "用户呼入",
"value": 7
},
{
"key": "无法接通",
"value": 8
},
{
"key": "非机主接听",
"value": 9
},
{
"key": "其他",
"value": 10
}
],
"Promise": [
{
"key": "承诺还款",
"value": 0
},
{
"key": "拒绝还款",
"value": 1
},
{
"key": "需要谈判",
"value": 2
},
{
"key": "无效沟通",
"value": 3
},
{
"key": "需要查找",
"value": 4
},
{
"key": "其他",
"value": 5
}
],
"CallerPhone": [
{
"key": "123123",
"value": 1
},
{
"key": "124312",
"value": 2
},
{
"key": "123213213",
"value": 3
},
{
"key": "12312312",
"value": 4
},
{
"key": "12312312",
"value": 5
}
],
"OperatorPhone": [
{
"key": "123123",
"value": 1
},
{
"key": "124312",
"value": 2
},
{
"key": "123213213",
"value": 3
},
{
"key": "12312312",
"value": 4
},
{
"key": "12312312",
"value": 5
}
],
"Marriage": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
},
{
"key": "数据未知",
"value": 5
}
],
"LoanUse": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
],
"ClientType": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
],
"CompanyType": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
],
"CompanyTypeName": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
],
"Live": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
],
"ClientRelationship": [
{
"key": "数据未知",
"value": 1
},
{
"key": "数据未知",
"value": 2
},
{
"key": "数据未知",
"value": 3
},
{
"key": "数据未知",
"value": 4
}
]
}