尝试解析JSON并创建提取的JSON

时间:2019-08-16 23:27:28

标签: json go

我试图根据从API接收的数据动态创建JSON对象。

收到的样本数据:将数据解组到下面给出的CiItems结构中

{
    "class_name": "test",
    "configuration_items": [
        {
            "id": "ea09a24f-01ef-42ad-ab19-e0369341d9b3",
            "ci_name": "makk",
            "comments": null,
            "created_by": "mike",
            "updated_by": "sam",
            "created": "2019-08-02T21:16:35.656Z",
            "updated": "2019-08-02T21:21:08.073Z",
            "ci_state_id": "randomid",
            "super_ci_id": null,
            "ci_attributes": [
                {
                    "attribute_id": "c995c693-b97c-4863-a61b-81a5d904c967",
                    "df_attribute_value": "xsmall",
                    "attribute_name": "tname",
                    "data_type": "string"
                },
                {
                    "attribute_id": "58845f48-7d2a-4c8c-8591-eaf59a23d84d",
                    "df_attribute_value": "vmware",
                    "attribute_name": "provider",
                    "data_type": "string"

                }
]}]}

下面是创建的结构:

 type Attribute struct {
    AttributeID      string `json:"attribute_id "`
    DfAttributeValue string `json:"df_attribute_value"`
    AttName          string `json:"attribute_name"`
    DataType         string `json:"data_type"`
}

// Attributes - array of Attribute
type Attributes []Attribute

// CiItem - confiuraion item of a VM
type CiItem struct {
    ID        string     `json:"ci_id"`
    Created   string     `json:"created"`
    Updated   string     `json:"updated"`
    CreatedBY string     `json:"created_by"`
    UpdatedBY string     `json:"updated_by"`
    Atts      Attributes `json:"ci_attributes"`
}

// CiItems - array of CiItem
type CiItems struct {
    ClassName string   `json:"class_name"`
    Items     []CiItem `json:"configuration_items"`
}

用于解组数据并创建提取的Json的代码:

func (client *Client) GetList() (CiItems, error) {
    var out CiItems
    err := client.doJsonRequest("GET",  &out)
    log.Info(out)
    if err != nil {
        return out, err
    }
    var output map[string]interface{}
    //parseMap(out.Items[0].(map[string]interface{}))
    extractBase(out.Items[0], &output)
    return out, nil
}


func extractBase(ci interface{}, output interface{}) {
    fields := reflect.TypeOf(ci)
    values := reflect.ValueOf(ci)
    num := fields.NumField()

    for i := 0; i < num; i++ {
        field := fields.Field(i)
        value := values.Field(i)

        if string(field.Name) != "Atts" {
            name := string(field.Name)
            output[name] = string(value)
        }
    }
}

我正在尝试创建一个ID为ci_name,created_by,updated_by,attribute_name的键值的JSON,如下所示:

{
  "ci_name": "makk",
  "created_by": "mike",
  "updated_by": "sam",
  "created": "2019-08-02T21:16:35.656Z",
  "updated": "2019-08-02T21:21:08.073Z",
  "tname": "xsmall",
  "provider": "vmware"
}

我尝试使用反射和其他方法

1 个答案:

答案 0 :(得分:1)

使用CiItem字段中的值创建地图,然后从函数中返回它。

func extractBase(ci *CiItem) map[string]interface{} {
    result := map[string]interface{}{
        "ci_name":    ci.Name,
        "created_by": ci.CreatedBY,
        "updated_by": ci.UpdatedBY,
        "created":    ci.Created,
        "updated":    ci.Updated,
    }
    for _, a := range ci.Atts {
        result[a.AttName] = a.DfAttributeValue
    }
    return result
}