Abhineet Srivastava

Abhineet Srivastava

  • NA
  • 890
  • 94.6k

DataList to json Conversion.

Sep 19 2013 1:13 AM
I have convert my Datalist to json formate like this--

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dtTable.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dtTable.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }
            string json = serializer.Serialize(rows);

o/p-

---------
[{"BusinessEntityID":1,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Ken","MiddleName":"J","LastName":"Sánchez","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"92c4279f-1207-48a3-8448-4636514eb7e2","ModifiedDate":"\/Date(1044642600000)\/"},{"BusinessEntityID":2,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Terri","MiddleName":"Lee","LastName":"Duffy","Suffix":null,"EmailPromotion":1,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"d8763459-8aa8-47cc-aff7-c9079af79033","ModifiedDate":"\/Date(1014489000000)\/"},{"BusinessEntityID":3,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Roberto","MiddleName":null,"LastName":"Tamburello","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"e1a2555e-0828-434b-a33b-6f38136a37de","ModifiedDate":"\/Date(1007490600000)\/"},{"BusinessEntityID":4,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Rob","MiddleName":null,"LastName":"Walters","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"f2d7ce06-38b3-4357-805b-f4b6b71c01ff","ModifiedDate":"\/Date(1009564200000)\/"},{"BusinessEntityID":5,"PersonType":"EM","NameStyle":false,"Title":"Ms.","FirstName":"Gail","MiddleName":"A","LastName":"Erickson","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"f3a3f6b4-ae3b-430c-a754-9f2231ba6fef","ModifiedDate":"\/Date(1012329000000)\/"},{"BusinessEntityID":6,"PersonType":"EM","NameStyle":false,"Title":"Mr.","FirstName":"Jossef","MiddleName":"H","LastName":"Goldberg","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"0dea28fd-effe-482a-afd3-b7e8f199d56f","ModifiedDate":"\/Date(1013884200000)\/"},{"BusinessEntityID":7,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Dylan","MiddleName":"A","LastName":"Miller","Suffix":null,"EmailPromotion":2,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"c45e8ab8-01be-4b76-b215-820c8368181a","ModifiedDate":"\/Date(1046802600000)\/"},{"BusinessEntityID":8,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Diane","MiddleName":"L","LastName":"Margheim","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"a948e590-4a56-45a9-bc9a-160a1cc9d990","ModifiedDate":"\/Date(1043260200000)\/"},{"BusinessEntityID":9,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Gigi","MiddleName":"N","LastName":"Matthew","Suffix":null,"EmailPromotion":0,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"5fc28c0e-6d36-4252-9846-05caa0b1f6c5","ModifiedDate":"\/Date(1044815400000)\/"},{"BusinessEntityID":10,"PersonType":"EM","NameStyle":false,"Title":null,"FirstName":"Michael","MiddleName":null,"LastName":"Raheem","Suffix":null,"EmailPromotion":2,"AdditionalContactInfo":null,"Demographics":"\u003cIndividualSurvey xmlns=\"http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/IndividualSurvey\"\u003e\u003cTotalPurchaseYTD\u003e0\u003c/TotalPurchaseYTD\u003e\u003c/IndividualSurvey\u003e","rowguid":"ca2c740e-75b2-420c-9d4b-e3cbc6609604","ModifiedDate":"\/Date(1054060200000)\/"},{
---------------------------------------------------------------------------


But i need nly firstname,Lastname,Middle name only..



Kindly help!

Answers (14)