dongqiaozhe5070 2017-06-20 00:58
浏览 46
已采纳

将JSON结构附加到JSON文件Golang

I have a JSON document of Users where they have an ID#, Phone#, and Email. Upon input of another ID, Phone, and Email, I want to take a new user's information and append it to the file. I have a structure that only contains an {ID: #, Phone: #, Email: #} and that's working fine. But my JSON file becomes as such:

[{"ID":"ABCD","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"EFGH","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"IJKL","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"MNOP","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"QRST","Phone":1234567890,"Email":"johndoe@test.com"}]
[{"ID":"UVWX","Phone":1234567890,"Email":"johndoe@test.com"}]

So I'm able to append to the document, but it's a new JSON structure enclosed by brackets []. Below is my code. I omitted the actual hashed ID.

func ToIds(e string, p int64) {
    hashed := GenId()
    var jsonText = []byte(`[
        {"ID": "", "Phone": 0, "Email": ""}
    ]`)
    var I Identification
    err := json.Unmarshal([]byte(jsonText), &I)
    if err != nil {
        fmt.Println(err)
    }
    I[0].ID = hashed
    I[0].Phone = p
    I[0].Email = e

    result, error := json.Marshal(I)
    if error != nil {
        fmt.Println(error)
    }

    f, erro := os.OpenFile("FILE_PATH", os.O_APPEND|os.O_WRONLY, 0666)
    if erro != nil {
        fmt.Println(erro)
    }

    n, err := io.WriteString(f, string(result))
    if err != nil {
        fmt.Println(n, err)
    }

}

This may be of use, here is my Identification struct.

type Identification []struct { ID string Phone int64
Email string }

Essentially, I want the outside brackets, and inside those brackets I want to append multiple users. Something like this:

    [
    {"id":"A", "phone":17145555555, "email":"tom@gmail.com"},
    {"id":"B","phone":15555555555,"email":"p@gmail.com"},
    {"id":"C","phone":14155555555,"email":"bradley@gmail.com"},
    {"id":"D","phone":17135555555,"email":"g@gmail.com"},
    {"id":"E","phone":17125555555,"email":"ann@gmail.com"},
    {"id":"F","phone":17125555555,"email":"sam@gmail.com"},
    {"id":"G","phone":14055555555,"email":"john@gmail.com"},
    {"id":"H","phone":13105555555,"email":"lisa@gmail.com"}
    ]
  • 写回答

1条回答 默认 最新

  • douxuzui4590 2017-06-20 01:08
    关注

    To achieve your output, define struct as follows-

    type Identification struct {
       ID    string
       Phone int64
       Email string
    }
    

    And perform operation as follows-

    // define slice of Identification
    var idents []Identification
    
    // Unmarshall it
    err := json.Unmarshal([]byte(jsonText), &idents)
    
    // add further value into it
    idents = append(idents, Identification{ID: "ID", Phone: 15555555555, Email: "Email"})
    
    // now Marshal it
    result, error := json.Marshal(idents)
    
    // now result has your targeted JSON structure
    

    Sample program of above explanation https://play.golang.org/p/67dqOaCWHI

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色