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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)