Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a code like this,

C#
DataTable dt = new DataTable();

string data = "{\"ProductId\":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77],\"ProductName\":[\"Chai\",\"Chang\",\"Aniseed Syrup\",\"Chef Anton's Cajun Seasoning\",\"Chef Anton's Gumbo Mix\",\"Grandma's Boysenberry Spread\",\"Uncle Bob's Organic Dried Pears\",\"Northwoods Cranberry Sauce\",\"Mishi Kobe Niku\",\"Ikura\",\"Queso Cabrales\",\"Queso Manchego La Pastora\",\"Konbu\",\"Tofu\",\"Genen Shouyu\",\"Pavlova\",\"Alice Mutton\",\"Carnarvon Tigers\",\"Teatime Chocolate Biscuits\",\"Sir Rodney's Marmalade\",\"Sir Rodney's Scones\",\"Gustaf's Knäckebröd\",\"Tunnbröd\",\"Guaraná Fantástica\",\"NuNuCa Nuß-Nougat-Creme\",\"Gumbär Gummibärchen\",\"Schoggi Schokolade\",\"Rössle Sauerkraut\",\"Thüringer Rostbratwurst\",\"Nord-Ost Matjeshering\",\"Gorgonzola Telino\",\"Mascarpone Fabioli\",\"Geitost\",\"Sasquatch Ale\",\"Steeleye Stout\",\"Inlagd Sill\",\"Gravad lax\",\"Côte de Blaye\",\"Chartreuse verte\",\"Boston Crab Meat\",\"Jack's New England Clam Chowder\",\"Singaporean Hokkien Fried Mee\",\"Ipoh Coffee\",\"Gula Malacca\",\"Rogede sild\",\"Spegesild\",\"Zaanse koeken\",\"Chocolade\",\"Maxilaku\",\"Valkoinen suklaa\",\"Manjimup Dried Apples\",\"Filo Mix\",\"Perth Pasties\",\"Tourtière\",\"Pâté chinois\",\"Gnocchi di nonna Alice\",\"Ravioli Angelo\",\"Escargots de Bourgogne\",\"Raclette Courdavault\",\"Camembert Pierrot\",\"Sirop d'érable\",\"Tarte au sucre\",\"Vegie-spread\",\"Wimmers gute Semmelknödel\",\"Louisiana Fiery Hot Pepper Sauce\",\"Louisiana Hot Spiced Okra\",\"Laughing Lumberjack Lager\",\"Scottish Longbreads\",\"Gudbrandsdalsost\",\"Outback Lager\",\"Flotemysost\",\"Mozzarella di Giovanni\",\"Röd Kaviar\",\"Longlife Tofu\",\"Rhönbräu Klosterbier\",\"Lakkalikööri\",\"Original Frankfurter grüne Soße\"]}";

dt = JsonConvert.DeserializeObject<DataTable>(data);


If I run this data on online json editor as remove `\` char, it's showing. But in Visual Studio, there is an error,

HTML
Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.


on last code place.

I want to, convert data to DataTable using Newtonsoft plugin. How can i solve this problem? Thanks.
Posted
Comments
Mehdi Gholam 11-Sep-14 5:32am    
You can't convert the above to a DataTable, try converting to an object or a dynamic object instead or even a normal dictionary.
Güray Yarar 11-Sep-14 5:35am    
If you have time, can you figure this code? I working until 2 days on this convert. :(
Mehdi Gholam 11-Sep-14 5:44am    
Use this site to check the validity of the json : http://jsonlint.com/
Güray Yarar 11-Sep-14 5:49am    
data is valid, check again. as remove \ char. Not problem. But I need this json data, convert to DataTable. But I don't know how can i write this code? I search, I can do like this. http://www.codeproject.com/Questions/590838/convertplusJSONplusstringplustoplusdatatable I write like this, but not working :(
ChauhanAjay 13-Sep-14 8:59am    
Try getting your data in this format
string data = @"[{""ProductId"":""1"",""ProductName"":""Chai""},"
+ @"{""ProductId"":""2"",""ProductName"":""Chang""},"
+ @"{""ProductId"":""3"",""ProductName"":""Aniseed Syrup""}]";
Hope this suggestion helps.

1 solution

Your json must be an array to be converted to a DataTable, it must start like this :
"[{\"ProductId\..........
end ends like this:
....,\"fieldName\":Value}]"
 
Share this answer
 

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