gpt4 book ai didi

java - 用JAVA将Json字符串插入MsSQL

转载 作者:行者123 更新时间:2023-12-01 12:33:02 24 4
gpt4 key购买 nike

我得到了 MsSQL server 2012 和具有固定架构的 Json 字符串。

我正在尝试将 Json 插入服务器,必须有一种方法可以自动解析 Json,然后插入所有值。

这就是 Json 的样子:

{
"UUID": "1408611728327",
"accuracy": 0,
"timestamp": 1408611668.274444000,
"x": -2.46,
"y": 24.779999,
"z": -17.46
}

最佳答案

也许用正则表达式重新格式化?所以就变成了这样:

insert into my_table values ('1408611728327',0,1408611668.274444000,-2.46,24.779999,-17.46);

这当然假设表字段的顺序正确。类似于:

myJson.replace("{","("); // you need round brackets instead of the json style ones
myJson.replace("}",")");
myJson.replaceAll("\".*\": ([^,]*),","$1"); // drops the name of the columns, only keeps the values, comma separated
myJson.replaceAll("\"","\'"); // sql strings are between ' not "
myJson= "insert into my_table values " + myJson + ";";

如果 json 具有 null 属性,因此不会显示,则会失败(如果 x 为 null,则 json 根本不包含 x,也许可以通过配置更改)。

关于java - 用JAVA将Json字符串插入MsSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25790809/

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