代码之家  ›  专栏  ›  技术社区  ›  motionpicturesoundtrack

从Go中的地图创建一个包含多个嵌套对象的json对象

  •  0
  • motionpicturesoundtrack  · 技术社区  · 3 年前

    如果我的问题措辞不当,我道歉。我是新手,尝试使用封送将一些数据格式化为JSON。我的函数看起来像:

    func writeJSON(w http.ResponseWriter, rep similarity.Similarity, keys []string) {
        for _, sub := range rep.Submissions {
            var jsonMap = make(map[string]interface{})
            vals := makeRow(sub)
            for i := range vals {
                jsonMap[keys[i]] = vals[i]
            }
            jsonData, err := json.Marshal(jsonMap)
            if err != nil {
                zap.L().Error(err.Error())
            }
            w.Write(jsonData)
        }
    }
    

    我基本上是通过为每次提交(sub-rep.submission)创建一个映射,添加我想要的键和值,并在完成后进行封送来获得键:值结构。然而,我得到的是单独的json对象,而不是一个。

    我当前的json响应格式如下所示:

    {
        "correct": "false",
        "learnerId": "student_03",
        "percentScore": "0.000000",
        "solutionCode": "x = 4",
        "solutionId": "515219a8",
        "submittedTime": "03/23/2022  05:58 PM UTC",
        "testsPassed": "0",
        "totalTests": "1"
    }{
        "correct": "false",
        "learnerId": "student_02",
        "percentScore": "0.000000",
        "solutionCode": "x = \"hello\";",
        "solutionId": "c5fe8f93",
        "submittedTime": "03/23/2022  05:57 PM UTC",
        "testsPassed": "0",
        "totalTests": "1"
    }{
        "correct": "true",
        "learnerId": "student_01",
        "percentScore": "0.000000",
        "solutionCode": "x = 2;",
        "solutionId": "c2be6a1f",
        "submittedTime": "03/23/2022  05:43 PM UTC",
        "testsPassed": "1",
        "totalTests": "1"
    }
    

    我想要这样的东西:

    {
        {
            "correct": "false",
            "learnerId": "student_03",
            "percentScore": "0.000000",
            "solutionCode": "x = 4",
            "solutionId": "asdad",
            "submittedTime": "03/23/2022  05:58 PM UTC",
            "testsPassed": "0",
            "totalTests": "1"
        },
        {
            "correct": "false",
            "learnerId": "student_02",
            "percentScore": "0.000000",
            "solutionCode": "x = \"hello\";",
            "solutionId": "asdasd",
            "submittedTime": "03/23/2022  05:57 PM UTC",
            "testsPassed": "0",
            "totalTests": "1"
        },
        {
            "correct": "true",
            "learnerId": "student_01",
            "percentScore": "0.000000",
            "solutionCode": "x = 2;",
            "solutionId": "asd",
            "submittedTime": "03/23/2022  05:43 PM UTC",
            "testsPassed": "1",
            "totalTests": "1"
        }
    }
    

    我试过使用jsonData,err:=json。封送(jsonMap)部分脱离for循环,但这不起作用。我也尝试过使用json。新编码器(w)。Encode(jsonMap),但这会产生与封送处理类似的结果。我能试试什么吗?非常感谢。

    1 回复  |  直到 3 年前
        1
  •  0
  •   Zombo    3 年前

    使用以下代码将映射封送到JSON数组:

    func writeJSON(w http.ResponseWriter, rep similarity.Similarity, keys []string) {
        var result []interface{}
        for _, sub := range rep.Submissions {
            var jsonMap = make(map[string]interface{})
            vals := makeRow(sub)
            for i := range vals {
                jsonMap[keys[i]] = vals[i]
            }
            result = append(result, jsonMap)
        }
        json.NewEncoder(w).Encode(result)
    }
    

    这段代码不会产生预期的结果,但它可能是您想要的。预期结果不是有效的JSON。