Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working in angular and I am making a call to WCF, which is further making a call to 'Business Layer' which is returning 'DataTable' data to WCF service. Now WCF needs to return the DataTable to Client side(.html page), so I am currently converting the DataTable to JSON string format and returning it to UI. At UI side, while making a Javascript object from the JSON string using angular.fromJson() function the sequence of column names gets changed and Javascript arrays are being made in alphabetical order. I do not want data to be alphabetically sorted when Javascript object is made.

So my query is as below:
1) How to prevent angular.fromJson() function from sorting data in JSON string in alphabetical order when it prepared JS Array from JSON string?

2) Is converting DataTable data to JSON string a good approach?; or I should use some other data format.

What I have tried:

This is the code snippet I am using:
{
var dimensionID = dimensionId;
var dataobj = JSON.stringify({ "dimensionId": 196 });
var response = commonservice.post(dataobj, 'ViewDimensionDetails');/
ShowLoader();
response.then(function (result) {
// result.data is JSON string returned by WCF
$scope.data = angular.fromJson(result.data);
}
Posted
Updated 5-Jul-17 23:45pm

1 solution

Convert DataTable to Generic List [^] and then use Json.NET [^] to convert the List to JSON[^].

Is converting DataTable data to JSON string a good approach?

No, It needs lot of formatting to get the exact output (handling nullable types, date etc )
 
Share this answer
 
Comments
knackCoder 6-Jul-17 12:43pm    
Actually in my case the data can contain any number of rows and columns as it is dynamically generated; Hence, in this case it is not possible to construct a Model Type for List.
I am working in angularjs, and data is returned in form of a DataTable which can contain any number of columns and rows; please suggest how should I proceed with it.
Karthik_Mahalingam 7-Jul-17 11:54am    
http://www.newtonsoft.com/json/help/html/SerializeDataSet.htm
knackCoder 7-Jul-17 22:55pm    
Thanks for sharing the article. Now I can convert my DataTable into JSON.
My requirement is as below and below mentioned are the JSON string I'm preparing for the data:


Data to Display which is dynamic. It can contain any number of rows or columns.

Skills| Behavior1|Behavior2
Skill1| A | B
Skill2| C | D

JSON string contain data and header together
[{“Skills”: “Skill1” , “Behavior1” : “A” , “Behavior2” : “B” } ,
{“Skills”: “Skill2” , “Behavior1” : “C” , “Behavior2” : “D” }]
JSON containing headers
[{“Skills”: “” , “Behavior1” : “” , “Behavior2” : “” }]

Now I am facing trouble while displaying this JSON data on UI. I use angular.fromJson function to make Javascript array objects of JSON string, but this function sorts objects properties in alphabetical order and I am not getting proper sequence in which data has to be displayed. Hope you have understood what I'm trying to achieve. Please let me know in case of any queries.
Karthik_Mahalingam 7-Jul-17 23:32pm    
am not getting.
knackCoder 8-Jul-17 2:22am    
I'm working in angularjs and I have to display data in tabular format as below:

Skills| Behavior1|Behavior2
-----------------------------
Skill1| A | B
Skill2| C | D

Above table is containing one row for header and other two rows for data. However, this tabular data is dynamic, i.e rows, header values, columns can vary as per data received from Database which is being returned as a DataTable, then in the WCF service I am converting the dataTable to JSON to send to client side where JSON is converted into Javascript object array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900