doumeilmikv7099 2018-04-04 13:41
浏览 240
已采纳

我如何以json格式输出map [string] interface {}?

I have a problem with formatting when using sqlx

rows, err = db.Queryx(query)
for rows.Next() {
    results := make(map[string]interface{})
    err = rows.MapScan(results)
    fmt.Fprintf(w,"%#v 
", results)
}

The output when using %#v

fmt.Fprintf(w,"%#v 
", results)

map[string]interface {}{"USER_ID”:”JD”, "USER_NAME”:”John Doe”}

map[string]interface {}{"USER_ID”:”JAD”, "USER_NAME”:”Jane Doe”}

map[string]interface {}{"USER_ID”:”DD”, "USER_NAME”:”Donald Duck”}

Using only %v

fmt.Fprintf(w,"%v 
", results)

map[USER_ID:JD USER_NAME:John Doe]

map[USER_ID:JAD USER_NAME:Jane Doe]

map[USER_ID:DD USER_NAME:Donald Duck]

The desired output is to get rid of map[string]interface {}

{"USER_ID”:”JD”, "USER_NAME”:”John Doe”}

{"USER_ID”:”JAD”, "USER_NAME”:”Jane Doe”}

{"USER_ID”:”DD”, "USER_NAME”:”Donald Duck”}

Is this possible?

EDIT USING JSON

According to @Timothy Jones I have updated the main code and it work as expected. Except that the result is printed to the TERMINAL and not browser.

rows, err = db.Queryx(query)
  for rows.Next() {
    results := make(map[string]interface{})
    err = rows.MapScan(results)
    if err := enc.Encode(results); err != nil {
      fmt.Fprintf(w,"%s
", results)
    }
  }

{"USER_ID”:”JD”,”USER_NAME”:”John Doe”}

{"USER_ID”:”JAD”,”USER_NAME”:”Jane Doe”}

{"USER_ID”:”DD”,”USER_NAME”:”Donald Duck”}

Removing the error handling just to test. It shows wrong results, but are printed to the BROWSER as it should.

rows, err = db.Queryx(query)
for rows.Next() {
  results := make(map[string]interface{})
  err = rows.MapScan(results)
  enc.Encode(results)
  fmt.Fprintf(w,"%s
", results)
}

map[USER_ID:JD USER_NAME:John Doe]

map[USER_ID:JAD USER_NAME:Jane Doe]

map[USER_ID:DD USER_NAME:Donald Duck]

One question remains. How do I print the correct result to the BROWSER?

  • 写回答

1条回答 默认 最新

  • douguabu8960 2018-04-04 17:00
    关注

    If you want to format your output as JSON, it's better to use the json package. There's a well-written introduction to json handling in this blog post, but generally the Marshal function is happy to take a map of map[string]interface{} where interface{} is any other type that it is able to marshal:

    b, err := json.Marshal(m)
    

    This returns a byte array, so you'll need to convert it to a string:

    m := map[string]string{
        "USER_ID":"JD", 
        "USER_NAME":"John Doe",
    }
    b, err := json.Marshal(m)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(b))
    

    https://play.golang.org/p/rffsuI4BL35

    However, since you know that you're just going to print out the encoded result, it's probably better to use a streamed encoder:

    m := map[string]string{
        "USER_ID":"JD", 
        "USER_NAME":"John Doe",
    }
    enc := json.NewEncoder(os.Stdout)
    
    if err := enc.Encode(m); err != nil {
            log.Fatal(err)
    }
    

    https://play.golang.org/p/l2-BOUK3yn9

    For your code, it looks like this:

    enc := json.NewEncoder(w)
    rows, err = db.Queryx(query)
    // Note: you should check err here
    
    for rows.Next() {
        results := make(map[string]interface{})
        err = rows.MapScan(results)
        // Note: You should check err here       
    
        if err := enc.Encode(results); err != nil {
             // Whatever you want to do in an encoding error
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包