Convert Java String returned from JDBC to Json object -
great genius people, need in solving java problem related java. in program making jdbc call , function returns string similar this:
[[{prod_cd=42, short_desc=waterfall edge}, {prod_cd=31, short_desc=n/a}, {prod_cd=51, short_desc=ogee edge}]]
i need rid of curly braces, commas, , save json object.
[ { "prod_cd": " 42", "short_desc": "waterfall edge", }, { "prod_cd": "31", "short_desc": "n/a", }, { "prod_cd": "51", "short_desc": "ogee edge", } ]
i highly appreciate help
here tried far:
@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();
//this returned following string:
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"); (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 j }end } catch (exception e) { logger.error("error trying jdbc"); } return edgecd; } }
this answer, not, answer want.
- look @ data returned database. notice has obvious pattern.
- there 3 basic objects in data returned database; array, container object, key-value pair. array array of container objects. container object contains 1 or more key-value pairs. key-value pair composed of 2 string values.
- create parser parses data returned database. recommend against attempting "fix" regular expression, parse code.
- using 3 objects mentioned above, create object representation of data using parser (also mentioned above).
- output objects json.
Comments
Post a Comment