duanping2005 2019-08-16 23:27
浏览 116
已采纳

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

I am trying to create a JSON object on the fly based on the data received from the API.

Sample Data received: Unmarshalled the data into CiItems struct given below

{
    "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"

                }
]}]}

Below are the structs created:

 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"`
}

Code to unmarshal the data and create a extracted 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)
        }
    }
}

I am trying to create a JSON with key value of id, ci_name, created_by, updated_by, attribute_name as below

{
  "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"
}

I have tried using reflect and other methods

  • 写回答

1条回答 默认 最新

  • doushibu2453 2019-08-17 00:00
    关注

    Create a map using values from CiItem fields and return it from the function.

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型