如何在golang

时间:2017-11-07 19:47:42

标签: arrays json go data-structures

如何解组json并填充结构。就像我有销售订单和销售订单结构一样。在json中,我将有1个salesorder记录和salesorderdetails结构的多个项目。

这是我尝试过单个项目和多个项目的go代码,但仅适用于salesorderdetails结构的单个记录。

Gocode:

package main

import (
    "encoding/json"
    "fmt"
)

type Order struct {
    SalesId             string `json:"sales_id"`
    Customer string `json:"customer_name"`
    TotalPrice   string `json:"totalprice"`
}
type OrderDetails struct {
    DetailId             string `json:"detail_id"`
    SalesId             string `json:"sales_id"`
    ItemName string `json:"item_name"`
    Qty   string `json:"qty"`
    Price   string `json:"price"`
}
type Temp struct {

    Salesorder Order  `json:"Salesorder"`
    Salesorderdetails OrderDetails  `json:"OrderDetails"`
}

func main() {
    jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":{"detailid":"1","sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"}}]`)

   //if i use above json it is working and if i use below json its not working
//jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":{"detailid":"1","sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"},{"detailid":"2","sales_id":"SOO2","item_name":"ITEM2","qty":"3","price":"200"}}]`)

    var temp []Temp
    err := json.Unmarshal(jsonByteArray, &temp)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%+v\n", temp)
    fmt.Printf("%+v\n ", temp[0].Salesorder.SalesId)
}

使用多个项目时出错:

panic: invalid character '{' looking for beginning of object key string

使用单项成功时输出:

[{Salesorder:{SalesId:SOO1 Customer:CUST1 TotalPrice:200} Salesorderdetails:{SalesId:SOO1 ItemName:ITEM1 Qty:2 Price:100}}]
SOO1

小提琴:使用salesorderdetails中的键进行了更新

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

2 个答案:

答案 0 :(得分:1)

你要解码的是无效的JSON。这是有效的:

{
    "Salesorder": {
        "sales_id": "SOO1",
        "customer_name": "CUST1",
        "totalprice": "200"
    },
    "OrderDetails": {
        "sales_id": "SOO1",
        "item_name": "ITEM1",
        "qty": "2",
        "price": "100"
    }
}

但你给的是这个:

{
    "Salesorder": {
        "sales_id": "SOO1",
        "customer_name": "CUST1",
        "totalprice": "200"
    },
    "OrderDetails": {
        "sales_id": "SOO1",
        "item_name": "ITEM1",
        "qty": "2",
        "price": "100"
    },
    {   // Things become invalid here
        "sales_id": "SOO2",
        "item_name": "ITEM2",
        "qty": "3",
        "price": "200"
    }
}

看起来您正在尝试提供对象列表,在这种情况下,您需要将这些对象包装在方括号[]中,否则您必须为第二个OrderDetails对象提供自己的密钥

答案 1 :(得分:0)

试试这个:

package main

import (
    "encoding/json"
    "fmt"
)

type Order struct {
    SalesId     string `json:"sales_id"`
    Customer    string `json:"customer_name"`
    TotalPrice  string `json:"totalprice"`
}

type OrderDetails struct {
    SalesId   string `json:"sales_id"`
    ItemName  string `json:"item_name"`
    Qty       string `json:"qty"`
    Price     string `json:"price"`
}

type Temp struct {
    Salesorder Order                  `json:"Salesorder"`
    Salesorderdetails []OrderDetails  `json:"OrderDetails"`
}

func main() {
    jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":[{"sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"},{"sales_id":"SOO2","item_name":"ITEM2","qty":"3","price":"200"}]}]`)

    var temp []Temp

    err := json.Unmarshal(jsonByteArray, &temp)
    if err != nil {
        panic(err)
    }

    //fmt.Printf("%+v\n", temp)

    // Printing all Orders with some details
    for _, order := range temp {
        fmt.Println("Customer:", order.Salesorder.Customer)

        for _, details := range order.Salesorderdetails {
            fmt.Printf("    ItemName: %s, Price: %s\n", details.ItemName, details.Price)
        }
    }
}

.. OrderDetails 现在是一个数组