Reputation: 109
I am returning a dataset from database in List and converting them into a JSON as below.
List<DensityGroup> dg = pdao.getProductPropListData("Density");
String data = new Gson().toJson(dg);
System.out.println(data);
I am facing below problems:
1. System.out
is printing the following in console.
[
{"densityId":"11","densityDescription":"Mcvr"},
{"densityId":"14","densityDescription":"test"}
]
I am getting the below response into browser with escape characters (I am making an AJAX call)
{"data":"[{\"densityId\":\"11\",\"densityDescription\":\"Mcvr\"},{\"densityId\":\"14\",\"densityDescription\":\"test\"}]"}
2. I need the following format. The extra quotes before [
is making my dataTable a mess-up.
{
"data": [
{"densityId":"11","densityDescription":"Mcvr"},
{"densityId":"14","densityDescription":"test"}
]
}
3. I don't need the escaping quotes before every double quotes in my current output. Please help me.
EDIT : Adding screenshots from Browser Console:
Upvotes: 0
Views: 1255
Reputation: 5699
This should work, if you're happy doing it within your Javascript:
var str = {"data":"[{\"densityId\":\"11\",\"densityDescription\":\"Mcvr\"},{\"densityId\":\"14\",\"densityDescription\":\"test\"}]"}
var data = JSON.parse(JSON.stringify(str).replace(/\\\"/g, '"').replace(/\"\[/g, '[').replace(/\]\"/g, ']'));
console.log(data);
It should work, but it might be better to fix it as it's coming from your server instead.
Upvotes: 1