aynakolik
aynakolik

Reputation: 99

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.

Upvotes: 1

Views: 15250

Answers (2)

Jim
Jim

Reputation: 31

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}]"

Upvotes: 3

asdf_enel_hak
asdf_enel_hak

Reputation: 7650

Your data cannot be converted to Datatabe

When I try your code it gives me:

Additional text found in JSON string after finishing deserializing object.

However when I do:

object obj  = JsonConvert.DeserializeObject(data); 

works fine.

You can also create your own class:

public class MyObject
{
    public int[] productId { get; set; }
    public string[] ProductName { get; set; }
}

MyObject obj = JsonConvert.DeserializeObject<MyObject>(data); 

So for different types of data you have, you can create different classes:

    public class MyObject2{
    ...
    }

    public class MyObject3{
    ...
    }
    ...

Object obj = null;
try{
 obj = JsonConvert.DeserializeObject<MyObject>(data); 
}catch{
      try{
      obj = JsonConvert.DeserializeObject<MyObject2>(data);
      }catch{
           try{
             obj = JsonConvert.DeserializeObject<MyObject3>(data);
           }catch{
             //Log error new type of data received
             throw new Exception();
           }
      }

}
if (obj.GetType() == typeof(MyObject)){
   ...
}else if (obj.GetType() == typeof(MyObject2)){
   ...
}
....

Upvotes: 0

Related Questions