dqce48404 2018-03-09 05:19
浏览 107
已采纳

根据长度为N的数组创建嵌套的JSON

I’ve been working to create a nested JSON from an Array but can’t seem to figure out how to do it. I currently have the following code, however its not working and cannot seem to fix it no matter what I do. The array that i’m currently working with look as follows. Note i’m trying to make JSON work no matter the array’s length.

[{2017-11-20 13:18:12 -0600 CST 70.261 2 1} {2017-11-20 13:11:15 -0600 CST 70.253 0 1} {2017-11-20 13:08:45 -0600 CST 70.43 0 1} {2017-11-20 13:05:29 -0600 CST 70.32000000000001 0 1} {2017-11-13 15:32:43 -0600 CST 76.354 0 1} {2017-11-13 15:26:41 -0600 CST 86.273 2 1} {2017-11-13 15:22:59 -0600 CST 86.273 2 1}][{2017-11-20 13:18:12 -0600 CST 70.261}]

The output i would like to would look something like this :

{
   "Status_message": "successful",
   "Weight_data": [{
   "Weight_date": "2017-11-17 15:22:59 -0600 CST",
    "Weight_kg": 86.273
}, {
    "Weight_date": "2017-11-14 15:22:59 -0600 CST",
    "Weight_kg": 85.273
}, {
    "Weight_date": "2017-11-12 15:22:59 -0600 CST",
    "Weight_kg": 76.273
}, {
    "Weight_date": "2017-11-16 15:22:59 -0600 CST",
    "Weight_kg": 66.273
 }]

My current code looks as follows, two structs

type AutoGenerated struct {
     StatusMessage string `json:"Status_message"`
     Weight_Data   [] Weight_Data
 }

type Weight_Data [] struct {
     Weight_Date string  `json:"Weight_date"`
     Weight_Kgs   float64 `json:"Weight_kg"`
}


func main() {


   var mainStruct AutoGenerated



   var current_weight [] Weight_Datas

   for i, _ := range m.ParseData().Weights {

        current_weight = Weight_Datas{m.ParseData().Weights[i].Date.String(),m.ParseData().Weights[i].Kgs}

        mainStruct.Weight_Datas = append(mainStruct.Weight_Datas, current_weight)


  }

    final := AutoGenerated{"Success",current_weight}

    js, err := json.Marshal(final)

 }
  • 写回答

1条回答 默认 最新

  • drep94225 2018-03-09 05:30
    关注

    You defined 'Weight_Data' as array of struct

    type Weight_Data [] struct {...}
    

    And used it as []Weight_Data field in AutoGenerated struct. This makes it as array of array of struct ie [][]struct{...}. I have corrected this mistake in below executable code sample.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type AutoGenerated struct {
        StatusMessage string `json:"Status_message"`
        Weight_Data   []Weight_Data
    }
    
    type Weight_Data struct {
        Weight_Date string  `json:"Weight_date"`
        Weight_Kgs  float64 `json:"Weight_kg"`
    }
    
    func main() {
    
        var current_weight []Weight_Data
    
        current_weight = append(current_weight, Weight_Data{
            Weight_Date: "2017-11-17 15:22:59 -0600 CST",
            Weight_Kgs:  86.273,
        })
    
        current_weight = append(current_weight, Weight_Data{
            Weight_Date: "2017-11-14 15:22:59 -0600 CST",
            Weight_Kgs:  85.273,
        })
    
        final := AutoGenerated{"Success", current_weight}
    
        js, err := json.Marshal(final)
        fmt.Println(string(js), err)
    
    }
    

    output:

    {"Status_message":"Success","Weight_Data":[{"Weight_date":"2017-11-17 15:22:59 -0600 CST","Weight_kg":86.273},{"Weight_date":"2017-11-14 15:22:59 -0600 CST","Weight_kg":85.273}]} <nil>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集