我在下面的例子中有一个包含sql insert命令的字符串。
我想解析字段名称和值并将它们转换为JSON。
如何使用Java实现这一目标?
我在SQL中的SQL命令:
String sqlQuery = "INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES " +
"(13, 'Phoenix', 'AZ', 33, 112); |\r\n" +
"INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES " +
"(44, 'Denver', 'CO', 40, 105); |\r\n" +
"INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES " +
"(66, 'Caribou', 'ME', 47, 68); |";
我期待如下输出:
{
"fields": [{
"ID": "13",
"CITY": "Phoenix",
"STATE": "AZ",
"LAT_N": "33",
"LONG_W": "112"
}, {
"ID": "44",
"CITY": "Denver",
"STATE": "CO",
"LAT_N": "40",
"LONG_W": "105"
}, {
"ID": "66",
"CITY": "Caribou",
"STATE": "ME",
"LAT_N": "47",
"LONG_W": "68"
}]
}
答案 0 :(得分:1)
这是将产生所需JSON格式输出的实际代码
我评论了每个部分的逻辑:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ToJson {
public static void main(String[] args) {
String sqlQuery = "INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES (13, 'Phoenix', 'AZ', 33, 112); |\r\n" +
"INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES (44, 'Denver', 'CO', 40, 105); |\r\n" +
"INSERT INTO myTable (ID, CITY, STATE, LAT_N, LONG_W) VALUES (66, 'Caribou', 'ME', 47, 68); |";
//REMOVE UNNEEDED STUFF
String trimUnwanted = sqlQuery.replaceAll("INSERT INTO myTable \\(ID, CITY, STATE, LAT_N, LONG_W\\) VALUES \\(", "")
.replaceAll("'", "")
.replaceAll("\\);", "");
System.out.println("afterTrim: " + trimUnwanted);
/*
afterTrim: 13, Phoenix, AZ, 33, 112 |
44, Denver, CO, 40, 105 |
66, Caribou, ME, 47, 68 |
*/
//SEPARATE INTO SEPARATE QUERIES
String[] choppedUpText = trimUnwanted.split("\\|");
System.out.println("Chopped up text >>");
Arrays.stream(choppedUpText).forEach(System.out::println);
//SEPARATE EACH QUERY INTO AN OBJECT, AND ADD TO LIST
List<Address> list = new ArrayList<>();
Arrays.stream(choppedUpText).forEach(s -> {
System.out.println("adding -> "+ s);
list.add(new Address(s.split(",")) ); //remember to do your error checking!
}) ;
String jsonStart = "{\n \"fields\": " ;
String jsonEnd = "\n\n}";
//THE ACTUAL RESULT
String json = jsonStart + list.toString() + jsonEnd;
System.out.println(json);
}
}
/* THE MAGIC CLASS HANDLES THE PARSING AND DISPLAY */
class Address {
String ID;
String CITY;
String STATE;
String LAT_N;
String LONG_W;
public Address(String[] input) {
this.ID = input[0].trim();
this.CITY = input[1].trim();
this.STATE = input[2].trim();
this.LAT_N = input[3].trim();
this.LONG_W = input[4].trim();
}
@Override
public String toString() {
//prints out in correct format
return " {\n" +
" \"ID\": \""+ ID+ "\",\n" +
" \"CITY\": \""+ CITY+ "\",\n" +
" \"STATE\": \""+ STATE+ "\",\n" +
" \"LAT_N\": \""+ LAT_N+ "\",\n" +
" \"LONG_W\": \""+ LONG_W+ "\"\n" +
" }";
}
}
上面代码的输出是:
afterTrim: 13, Phoenix, AZ, 33, 112 |
44, Denver, CO, 40, 105 |
66, Caribou, ME, 47, 68 |
Chopped up text >>
13, Phoenix, AZ, 33, 112
44, Denver, CO, 40, 105
66, Caribou, ME, 47, 68
adding -> 13, Phoenix, AZ, 33, 112
adding ->
44, Denver, CO, 40, 105
adding ->
66, Caribou, ME, 47, 68
{
"fields": [ {
"ID": "13",
"CITY": "Phoenix",
"STATE": "AZ",
"LAT_N": "33",
"LONG_W": "112"
}, {
"ID": "44",
"CITY": "Denver",
"STATE": "CO",
"LAT_N": "40",
"LONG_W": "105"
}, {
"ID": "66",
"CITY": "Caribou",
"STATE": "ME",
"LAT_N": "47",
"LONG_W": "68"
}]
}
答案 1 :(得分:1)
这是关于如何做到这一点的一种方法。这假设列名称是动态的。
fields
字段的类。java.util.Pattern
+ jave.util.Matcher
在()
之间提取项目。您将获得3个匹配,每个匹配将有2个组,一个具有列名称,另一个具有te值。<column, column value>
放入地图中。将此地图添加到列表中。fields
值。GSON
或Jackson
库将第5步中的实例转换为JSON。