gpt4 book ai didi

json - 将具有可变类型的json转换为字符串

转载 作者:IT王子 更新时间:2023-10-29 01:41:50 24 4
gpt4 key购买 nike

我正在从 API 响应中读取 json,我遇到了一个问题,因为 json 值中有多种数据类型(字符串、空值、 bool 值)。此外,一些键的值可以是字符串或空值,这使得将数据读入类型更加困难。我想将所有内容都转换为字符串以便于处理。我根据谷歌搜索其他示例创建了一个类型开关。我想知道这是否是最简单的方法,或者我是否缺少更简单的方法。

  package main

import (
"encoding/json"
"fmt"
"strconv"
)

func main() {

json_byte := []byte(`{"response":[{"t_int":1, "t_bool": true, "t_null_or_string": null}, {"t_int":2, "t_bool": false, "t_null_or_string": "string1"}]}`)

//unmarshal the json to data structure using interface for variable data types
data_json := make(map[string][]map[string]interface{}) //create a structure to hold unmarshalled json
if err := json.Unmarshal(json_byte, &data_json); err != nil {
panic(err)
}
fmt.Println("json_data: ", data_json)

//Iterate over data structure and convert Bool, Int, and Null types to string
var v_conv string // temporary holding for converted string values
data_map := make(map[string]string) // temporary holding for converted maps
data_final := make([]map[string]string, 0, 100) // final holding for data converted to strings

for _, v := range data_json { //v is the value of the "response": key which is a slice of maps
for _, v2 := range v { //v2 is one of the maps in the slice of maps
for k3, v3 := range v2 { //k3 and v3 are the keys and values inside the map
fmt.Println("k3: ", k3, "v3: ", v3)
switch v_type := v3.(type) {
case nil:
v_conv = ""
case bool:
v_conv = strconv.FormatBool(v3.(bool))
case int:
v_conv = strconv.Itoa(v3.(int))
case string:
v_conv = v3.(string)
case float64:
v_conv = strconv.FormatFloat(v3.(float64), 'f', 0, 64)
default:
fmt.Println("vtype unknown: ", v_type) //have to use v_type since it is declared
v_conv = ""
}
data_map[k3] = v_conv //append a new map key/value pair both as strings
fmt.Println("data_map: ", data_map)
}
data_final = append(data_final, data_map) // after each cycle through the loop append the map to the new list
fmt.Println("data_final: ", data_final)
}
}
}

最终格式需要一片 map [{ “t_int”:“1”, “t_bool”:“真”, “t_null_string”:“” }, { “t_int”:“2”, “t_bool”:“假”, “t_null_string”:“string1” }]

最佳答案

对于这个答案,我假设您示例中的 JSON 是您的 JSON 输入的(部分)示例。在这种情况下,您的 JSON 具有特定的结构:您知道哪些属性带有已知数据类型,并且您还知道哪些属性是动态的。例如,您可以像下面的 ResponseObj 一样将 JSON 解码为 smth:

package main

import (
"encoding/json"
"fmt"
)

type ResponseObj struct {
Response []Item `json:"response"`
}

type Item struct {
TInt int `json:"t_int"`
TBool bool `json:"t_bool"`
TMixed interface{} `json:"t_null_or_string"`
}

func main() {

json_byte := []byte(`{"response":[{"t_int":1, "t_bool": true, "t_null_or_string": null}, {"t_int":2, "t_bool": false, "t_null_or_string": "string1"}]}`)

data_json := ResponseObj{}
if err := json.Unmarshal(json_byte, &data_json); err != nil {
panic(err)
}
fmt.Printf("%+v\n", data_json)
}

您的数据将如下所示:

{
Response:
[
{
TInt:1
TBool:true
TMixed:<nil>
}
{
TInt:2
TBool:false
TMixed:string1
}
]
}

是的,对于具有混合类型的属性,您将运行类型断言(或在您的情况下与 nil 进行比较,或两者都进行)。

您的 JSON 不太可能是一团乱七八糟的不可预测的类型。最有可能的是,您可以挑出一个核心结构,然后将接口(interface){}用于剩余的混合类型。

希望这对您有所帮助。

关于json - 将具有可变类型的json转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40988252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com