I have a database table with JSONB datatype as one of the column. My application is in Struts2 which uses struts2-json.jar to map deal the request and response as JSON.
The problem I am facing is that i wish to send the JSON data from a table column as JSON array in the response. However i end up sending the data as Hash-literal.
When the method returns SUCCESS,there's a HashMap associated which is used to return the data as JSON.
I am putting the name of column as key and the value of that column as value for that key in the HashMap.
However, when struts2-json library is converting the value of the JSON key as follows.
{
"status":"success",
"data":[
{
"sem_votes_allowed":{
"type":"jsonb",
"value":"{\"sem_votes_allowed\": [{\"E1\": \"100\", \"E2\": \"200\"}]}"
},
"uvp_even_id":{
"type":"jsonb",
"value":"[\"E1\", \"E2\"]"
},
"usm_x_mobile_no":null,
"usm_pin_no":null,
"sem_first_even_flag":null,
"sem_holdings":{
"type":"jsonb",
"value":"{\"sem_holdings\": [{\"E1\": \"100\", \"E2\": \"200\"}]}"
}
}
],
"action":"sc/get_shareholder_list",
"statusmsg":"Share holder list",
"statuscode":"E_OK"
}
And the response i wish to send is
{
"status":"success",
"data":[
{
"sem_votes_allowed":{
"sem_votes_allowed":[
{
"E1":"100",
"E2":"200"
}
]
},
"uvp_even_id":[
"E1",
"E2"
],
"usm_x_mobile_no":null,
"usm_pin_no":null,
"sem_first_even_flag":null,
"sem_holdings":{
"sem_holdings":[
{
"E1":"10",
"E2":"200"
}
]
}
}
],
"action":"sc/get_shareholder_list",
"statusmsg":"Share holder list",
"statuscode":"E_OK"
}
"sem_votes_allowed" , "uvp_even_id", and "sem_holdings" are the columns in the table with datatype as JSONB.
Below is the code snippet where in the HashMap is getting populated.
ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
while (rs.next()) {
HashMap<String, Object> record = new HashMap<String, Object>();
for (int i = 1; i < numColumns + 1; i++) {
String columnName = rsmd.getColumnName(i);
record.put(columnName, rs.getObject(columnName));
}
out.add(record); // ArrayList out
}
shareDataMap is an instance member and has its getter and setter
HashMap <String,Object> shareDataMap = new HashMap<>();
shareDataLs = conn.executeQuery(query, params);
System.out.println("shareDataLs: " + shareDataLs);
shareDataMap.put(Constants.SUCCESS, Constants.SUCCESS);
shareDataMap.put(Constants.DATA, shareDataLs);
This is the code snippet in struts.xml
<action name="get_shareholder_list" method="shareHldrLs"
class="co.merce.instapoll.ui.ShareHldrAction">
<result type="json" name="success">
<param name="root">shareDataMap</param>
</result>
</action>
It would be really helpful if anybody can show me the workaround to achieve this.
Thanks in advance.