【问题标题】:How to print json value in golang [closed]如何在golang中打印json值[关闭]
【发布时间】:2019-06-07 15:52:45
【问题描述】:

我有一些 json 并想打印值,但我不知道如何从这样的 json 格式打印

"order_items":[  
      {  
         "total":1,
         "unitprice":1,
         "price":1,
         "create_date":"2019-06-07 13:51:36",
         "flow_no":"1234",
         "code":"4567",
         "quantiry":1,
         "discount_ctotal":0,
         "img":"",
         "fname":"first_name",
         "specs":"256"
      }
   ],

如何从中打印code 值?

【问题讨论】:

标签: json go


【解决方案1】:

您必须创建一个struct,其中包含您要查找的数据。如果您只关心code,那么这就是您需要定义的全部内容。

type OrderItem struct {
    Code string `json:"code"`
}

然后将您的 JSON 解组为 OrderItems 的一部分。

var orderItems []OrderItem
if err := json.Unmarshal(yourJson, &orderItems); err != nil {
    // handle errors in deserialization
}

然后对输出做任何你想做的事情。

for _, orderItem := range orderItems {
    code := orderItem.Code
    // do something with it? I don't know
    fmt.Println(code)  // I guess?
}

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 2019-07-05
    相关资源
    最近更新 更多