如何将json对象的json数组合并到单个json对象 [英] How to merge a json array of json objects to a single json object

查看:110
本文介绍了如何将json对象的json数组合并到单个json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要合并对象的json数组,以便附加相同键的值可以说我有一个未知的json数组,例如

I need to merge json array of objects such the values of same keys are appended Lets say I have an unknown json array like

"jsonarray": [
    {
      "behavior": [
        "file",
        "create_doc_exe"
      ],
      "id": 3303511,
      "platform": "Windows 7 x64 SP1, Adobe Reader 11, Flash 11, Office 2010"
    },
    {
      "info": [
        "sign , 3c4e53e "
      ],
      "behavior": [
        "sys_folder",
        "file",
        "process",
        "crash"
      ],
      "id": 3303,
      "platform": "Windows XP, Adobe Reader 9.4.0, Flash 10, Office 2007"
    }
  ]

我希望结果是这样的:

"jsonarray": {
    "behavior": [
        "file",
        "create_doc_exe",
        "sys_folder",
        "file",
        "process",
        "crash"
    ], 
    "id": [3303511,3303], 
    "platform": [
        "Windows 7 x64 SP1, Adobe Reader 11, Flash 11, Office 2010",
        "Windows XP, Adobe Reader 9.4.0, Flash 10, Office 2007"
    ], 
    "info": ["sign , 3c4e53e "] 
}

推荐答案

仅使用标准库,这应该可以解决问题:

Using only the standard library, this should do the trick:

package main

import (
    "encoding/json"
    "fmt"
)

var content = `{
    "jsonarray": [
    {
      "behavior": [
        "file",
        "create_doc_exe"
      ],
      "id": 3303511,
      "platform": "Windows 7 x64 SP1, Adobe Reader 11, Flash 11, Office 2010"
    },
    {
      "info": [
        "sign , 3c4e53e "
      ],
      "behavior": [
        "sys_folder",
        "file",
        "process",
        "crash"
      ],
      "id": 3303,
      "platform": "Windows XP, Adobe Reader 9.4.0, Flash 10, Office 2007"
    }
  ]
}`

type payload struct {
    JSONArray []map[string]interface{} `json:"jsonarray"`
}

func main() {
    var objPayload = payload{
        []map[string]interface{}{},
    }

    if err := json.Unmarshal([]byte(content), &objPayload); err != nil {
        panic(err)
    }

    var result = map[string]interface{}{}

    for _, item := range objPayload.JSONArray {
        for k, v := range item {
            var ok bool

            // If this is the first time our key is brought up, let's just copy it to the final map
            if _, ok = result[k]; !ok {
                result[k] = v
                continue
            }

            // It's not the first time this key shows up, let's convert it to a slice if it's still not
            if _, ok = result[k].([]interface{}); !ok {
                result[k] = []interface{}{result[k]}
            }

            // Then let's ensure our value is also a slice
            if _, ok = v.([]interface{}); !ok {
                v = []interface{}{v}
            }

            // Appending it to the result
            result[k] = append(
                result[k].([]interface{}),
                v.([]interface{})...,
            )
        }
    }

    if resultBytes, err := json.Marshal(&result); err != nil {
        panic(err)
    } else {
        fmt.Printf("%s", resultBytes) //done!
        // Result should be {"behavior":["file","create_doc_exe","sys_folder","file","process","crash"],"id":[3303511,3303],"info":["sign , 3c4e53e "],"platform":["Windows 7 x64 SP1, Adobe Reader 11, Flash 11, Office 2010","Windows XP, Adobe Reader 9.4.0, Flash 10, Office 2007"]}
    }
}

这篇关于如何将json对象的json数组合并到单个json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆