0

I know this kind of question has been asked before but I cannot seem to find anything that is working.

I am trying to convert a JSON string to a DataTable, or any other form that I can work with on C#.

Here is an example of the JSON file I receive:

{ "DatabaseName" : {"Employees": [{"Full-Name":"John Smith","Address":["123 Main St", "Apt 202"],"City":"NYC"}]}}

I have tried different things, but I either get an empty DataTable, or many different errors.

Object jObject = JsonConvert.DeserializeObject<JObject>(Response.Content);
DataTable dsResult = JsonConvert.DeserializeObject<DataTable>(jObject.ToString());

Gives me:

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

Any help would be appreciated.

I have used JSON 2 C# to create a class as well, and trying to convert it using

JsonConvert.DeserializeObject<Employees>(Response.Content);

However the classes do not allow me to create a variable public string Full-Name since I cannot use a - when creating a variable.

Is there a workaround for this?

user130622
  • 53
  • 2
  • 8
  • 1
    possible duplicate of [Convert JSON to DataTable](http://stackoverflow.com/questions/11981282/convert-json-to-datatable) – Ňɏssa Pøngjǣrdenlarp Apr 10 '15 at 13:20
  • You shouldn't convert it to a DataTable, you should convert it to a model object (a plain class). To figure out what the class structure should be, I like [http://json2csharp.com/](http://json2csharp.com/) – mason Apr 10 '15 at 13:23
  • When doing that, I get the following error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ProDataSet]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type – user130622 Apr 10 '15 at 13:39
  • @user130622 You have to make sure you deserialize to the correct class. – mason Apr 10 '15 at 16:15

2 Answers2

1

That JSON is invalid, use this to check: http://jsonlint.com/

should be something like

{
    "DatabaseName": "Employees",
    "Rows": [
        {
            "FullName": "John Smith",
            "Address": [
                "123 Main St",
                "Apt 202"
            ],
            "City": "NYC"
        }
    ]
}
NeonBoxx
  • 143
  • 5
  • Fixed the JSON string on this example. I was missed an opening bracket when typing the json string on here. But the string is a valid json. – user130622 Apr 10 '15 at 13:49
  • you could deserialize this to a Dictionary – NeonBoxx Apr 10 '15 at 14:04
  • On the converted class from json2csharp you can decorate the fullname property with this attribute: `[Newtonsoft.Json.JsonProperty(PropertyName="Full-Name")] public string FullName { get; set; }` – NeonBoxx Apr 10 '15 at 14:12
0

Use Json 2 C# to create your class and like @Plutonix said look here for the Data Table.

Community
  • 1
  • 1
C1rdec
  • 1,647
  • 1
  • 21
  • 46