Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

c# - Newtonsoft.Json JsonConvert To Datatable

I have a code like this,

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,

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your json must be an array of objects to be converted to a DataTable, it must start like this :

"[{"ProductId..........

end ends like this:

....,"fieldName":Value}]"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...