在数据库中保存 json 格式的字符串时,我是否也应该保存值的类型以便稍后将其转换回来?

when saving json formatted string on database, should i also save the type of value to convert it back later?

我正在使用 rapidjson on c++ to convert some numbers and strings to json 格式。 然后将它们作为文本保存在 mysql table 的列中。

稍后当我想将该文本转换为实际值时:

感谢即将到来的帮助。

how do i know the value type of the TEXT?

您可以检查它是字符串、浮点数、整数、布尔值、空值还是数组。

does json do that for me? convert to the actual type?

JSON 只是一个符号,它不做任何事情。但是您解析值的库可能会这样做。例如,字符串将用引号引起来。或者数组将被包裹在方括号中。整数和浮点数是空的但有效的数字。

should i have to know the type in advance?

这取决于您的应用程序。当您构建 JSON 时,您可能知道每个键的类型,因此当您解析 JSON 时,您可以使用该信息。

阅读Rapid的文档JSON,它可以解析和推断值的类型。所以,只要正确使用这个库就够了。