I have some json and want to print value but I don't know how to print from json format like this
"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"
}
],
How can I print code
value from this?
You'll have to make a struct
that has the data you're looking for. If all you care about is the code
, then that's all you need to define.
type OrderItem struct {
Code string `json:"code"`
}
Then just unmarshal your JSON into a slice of OrderItem
s.
var orderItems []OrderItem
if err := json.Unmarshal(yourJson, &orderItems); err != nil {
// handle errors in deserialization
}
Then do whatever you want with the output.
for _, orderItem := range orderItems {
code := orderItem.Code
// do something with it? I don't know
fmt.Println(code) // I guess?
}