JSON String Format for Data Table
JSON String Format for Data Table
adevade
Posts: 8Questions: 1Answers: 0
hello Iam using a vb.net service in which I am converting a Data view to JSON String, but the concern is that How will I convert the string into Data Table Compatible String Help me out with a solution.
Dim dvDataVw As New DataView(SendDataTable)
SendDataTable = dvDataVw.ToTable
json = JsonConvert.SerializeObject(SendDataTable, Formatting.Indented)
This question has accepted answers - jump to:
This discussion has been closed.
Answers
What JSON does the above result in? DataTables can read most JSON formats.
Allan
[
{
"dbName": "DEV12SEP",
"cmpName": "BLANK12SEP",
"versStr": "920180",
"dbUser": "dbo",
"LOC": "United States Of America/Puerto Rico"
},
{
"dbName": "BLANKDBPL8",
"cmpName": "BLANKDBPL8",
"versStr": "920180",
"dbUser": "dbo",
"LOC": "United States Of America/Puerto Rico"
},
{.....}
]
That looks good to me. See this example for how you can have DataTables read JSON in that form (i.e. a flat array of objects).
Allan
yes Allen it worked for me when I placed this hardcoded JSON into a txt File,but I am here throwing this same string from server side, what chnages I have to make to achieve that.
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[
{
"dbName": "DEV12SEP",
"cmpName": "BLANK12SEP",
"versStr": "920180",
"dbUser": "dbo",
"LOC": "United States Of America/Puerto Rico"
},
{
"dbName": "BLANKDBPL8",
"cmpName": "BLANKDBPL8",
"versStr": "920180",
"dbUser": "dbo",
"LOC": "United States Of America/Puerto Rico"
},
{...}
]
</string>
//Because I am sending a variable which is of String type from server side, i.e by Serializind a DataTable into JSON
Can you tell the framework not to encapsulate it in a string? I'm afraid that is outside my area of expertise. You'd need to ask of SO or similar.
As it stands, the server isn't returning valid JSON which is why DataTables is rejecting it.
Allan
Dim dvDataVw As New DataView(SendDataTable) //SendDatatable contains the the Data Table in it
//To convert the Data Table into the JSON format, the format here looks perfect but when this json returns to client end it gives problem i.e Data Table gives Invalid JSON error, but my JSON have a valid String form
Have you tried pasting the returned JSON into https://jsonlint.com/ to verify its a valid format?
Kevin
Yes Kevin its a valid JSON format, and only creates problem when thrown to client end (i.w to DataTable) as it is (actually the string contains in a variable which is of
String type and this variable is thrown to Client end directly after hitting the URL), You can see the JSON format attached as snaptinypic.com/r/fay5b8/9
If I understand what Allan is saying this part:
Is causing an issue with Datatables. The rest of the format looks good. I think he is asking for the string encapsulation to be removed.
Kevin
Correct. The entire response from the server needs to be valid JSON - not just part of it. That XML tag is making it not valid JSON.
Allan
After some trails ,We achieved the Data putting into the table from Server side...
Thanks for your support.
$.ajax({
type: "GET",
url: [Service IP],
crossDomain: true,
dataType: "json",
async: false,
cache: false,
success: function (data, status, xhr) {
//console.log("From Server-->"+data);
var objData = new Object();
objData = JSON.parse(data);
//console.log("--->parse"+objData);
$('#example').DataTable({
//"dataSrc": objData,
data: objData,
//"serverSide": true,
// "processing": true,
"columns": [
{ "data": "dbName" },
{ "data": "cmpName"},
{ "data": "versStr"},
{ "data": "dbUser"},
{ "data": "LOC"}
]
});
},
error: function (xhr, status, error) {
alert('Error Occured - ' + error);
}
});
Good to hear - thanks for letting us know.
Allan