伟大的天才人,我需要帮助解决与java相关的java问题。在一个程序中,我正在进行JDBC调用,该函数返回一个类似于此的字符串:
[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]
我需要摆脱花括号,逗号,并将其保存为json对象。
[
{
"PROD_CD": " 42",
"SHORT_DESC": "WATERFALL EDGE",
},
{
"PROD_CD": "31",
"SHORT_DESC": "N/A",
},
{
"PROD_CD": "51",
"SHORT_DESC": "OGEE EDGE",
}
]
我非常感谢您的帮助
这是我到目前为止所尝试的内容:
@Override
public Map<String, String> getEdgeCd() {
Map<String, String> EdgeCd = new HashMap<String, String>();
Map<String,Object> temp = new HashMap<String,Object>();
try {
SimpleJdbcCall fgetEdgeCd = new SimpleJdbcCall(jdbcTemplate)
.withSchemaName("logprd")
.withCatalogName("edge_api_pkg")
.withFunctionName("DDGetEdgeCd");
temp = fgetEdgeCd.execute();
System.out.println("temp " + temp + " \n\n\n\n\n\n");
System.out.println("temp.values() " + temp.values() + " lines \n\n\n\n\n\n");
String keyList = temp.values().toString();
//这是以下列字符串返回:
String keyList = "[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]";
String[] currentLine;
currentLine = keyList.substring(3, keyList.length() -3).split("[}]|[{]");
String currenLineString = Arrays.toString(currentLine);
String newCurrentLineString = currenLineString.substring(1, keyList.length()-1).replaceAll("," , "").replaceAll(" EDGE" , "-Edge").replaceAll("\\s+", " ");
//System.out.println("newCurrentLineString:>"+ newCurrentLineString + "\n\n");
String[] testLine;
testLine = newCurrentLineString.split(" ");
ArrayList<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String, Object>>();
LinkedHashMap<String, Object> map=new LinkedHashMap<String, Object>();
Collection<String> keyValue = null;
for(int i=0; i< testLine.length; i++) {
String[] temp = testLine[i].toString().split("=");
keyValue.addAll(Arrays.asList(temp));
System.out.println( "keyValue"+i + ":>"+ keyValue.toString() + "\n\n");
for (int j=0; j < keyValue.size(); j+=2) {
map.put(temp[j].toString(), temp[j+1].toString());
//map.put(keyValue[i].toString(), keyValue[i+1].toString());
//System.out.println( "mapmain:>"+ map.toString() + "\n\n");
data.add(map);
System.out.println(map.toString());
System.out.println(data.toString());
}//end for j
}end for i
} catch (Exception e) {
logger.error("Error trying JDBC");
}
return EdgeCd;
}
}
答案 0 :(得分:0)
这是一个答案, 但不一定是你想要的答案。
答案 1 :(得分:0)
这是C#中的一个快速功能,可以轻松移植到JAVA(抱歉,我的手机上没有JAVA编译器)。如果大括号或逗号出现在记录数据中,主要问题将是假设空格和转义字符。
string data = "[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]";
char[] chars = data.ToCharArray();
StringBuilder currentRecord = null;
StringBuilder json = new StringBuilder("[");
bool isInCurly = false;// Loop state
for (int i=0;i<chars.Length; ++i)
{
if (chars[i] == '{')
{
isInCurly = true;
currentRecord = new StringBuilder("{");
}
else if (chars[i] == '}')
{
currentRecord.Append("}");
isInCurly = false;
// Major assumptions made here about the structure that will need to be verified such as ", " between record values, etc...
string cleanRecord = currentRecord.ToString().Replace("{", "{\"")
.Replace("=", "\":\"")
.Replace(", ", "\", \"")
.Replace("}", "\"}");
json.AppendLine(cleanRecord + ", ");
}
else if(isInCurly)
{
currentRecord.Append(chars[i]);
}
}
json.Append("]");
string finalJson = json.ToString();