将JDBC返回的Java字符串转换为Json对象
伟大的天才们,我需要帮助解决与java相关的java问题。在一个程序中,我正在进行JDBC调用,该函数返回一个类似以下内容的字符串:将JDBC返回的Java字符串转换为Json对象,java,json,Java,Json,伟大的天才们,我需要帮助解决与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": "
[[{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();
@覆盖
公共地图getEdgeCd(){
Map EdgeCd=newhashmap();
Map temp=newhashmap();
试一试{
SimpleJdbcCall fgetEdgeCd=新SimpleJdbcCall(jdbcTemplate)
.使用Schemaname(“logprd”)
.withCatalogName(“edge\u api\u包装”)
.withFunctionName(“DDGetEdgeCd”);
temp=fgetEdgeCd.execute();
System.out.println(“temp”+temp+”\n\n\n\n\n);
System.out.println(“临时值()”+临时值()+“行\n\n\n\n\n”);
字符串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;
}
}
String keyList=“[{PROD_CD=42,SHORT_DESC=瀑布边缘},{PROD_CD=31,SHORT_DESC=N/A},{PROD_CD=51,SHORT_DESC=OGEE边缘}]”;
字符串[]当前行;
currentLine=keyList.substring(3,keyList.length()-3);
字符串currenLineString=Arrays.toString(currentLine);
String newCurrentLineString=currenLineString.substring(1,keyList.length()-1).replaceAll(“,”,”).replaceAll(“边”,“边”).replaceAll(“\\s+”,”);
//System.out.println(“newCurrentLineString:>”+newCurrentLineString+“\n\n”);
字符串[]测试线;
testLine=newCurrentLineString.split(“”);
ArrayList数据=新的ArrayList();
LinkedHashMap=新建LinkedHashMap();
集合keyValue=null;
for(int i=0;i”+keyValue.toString()+“\n\n”);
对于(int j=0;j”+map.toString()+“\n\n”);
数据。添加(地图);
System.out.println(map.toString());
System.out.println(data.toString());
}//结束于j
}我的结局
}捕获(例外e){
error(“尝试JDBC时出错”);
}
返回EdgeCd;
}
}
这是一个答案,
但不一定是你想要的答案
这里有一个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();
string data=“[{PROD_CD=42,SHORT_DESC=瀑布边},{PROD_CD=31,SHORT_DESC=N/A},{PROD_CD=51,SHORT_DESC=OGEE边}]”;
char[]chars=data.ToCharArray();
StringBuilder currentRecord=null;
StringBuilder json=新的StringBuilder(“[”);
bool isInCurly=false;//循环状态
对于(int i=0;i,这里有一个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();
string data=“[{PROD_CD=42,SHORT_DESC=瀑布边},{PROD_CD=31,SHORT_DESC=N/A},{PROD_CD=51,SHORT_DESC=OGEE边}]”;
char[]chars=data.ToCharArray();
StringBuilder currentRecord=null;
StringBuilder json=新的StringBuilder(“[”);
bool isInCurly=false;//循环状态
对于(int i=0;i)您是否尝试过编写一些代码?是您编写的方法还是数据库提供的字符串?如果是,您使用的是哪个数据库?@AbubakkarRangara我尝试过some@Thomas这是JDBC从SQL端返回的数据库。Abubakkar的意思是:您尝试了什么(告诉我们/向我们展示)?那么您正在使用SQL数据库?这没有多大帮助。请添加更多信息(例如,数据库和版本、您正在使用的查询等)为了让我们能提供帮助。你试过写一些代码吗?是你写的那个方法还是数据库提供的字符串?如果是,你在使用哪个数据库?@AbubakkarRangara我试过了some@Thomas这是JDBC从SQL端返回的数据库