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?

4
  • 1
    possible duplicate of Convert JSON to DataTable Commented Apr 10, 2015 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/ Commented Apr 10, 2015 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 Commented Apr 10, 2015 at 13:39
  • @user130622 You have to make sure you deserialize to the correct class. Commented Apr 10, 2015 at 16:15

2 Answers 2

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"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
you could deserialize this to a Dictionary<string,Object>
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; }
0

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

4 Comments

I expanded the question. Json 2 C# gives me __invalid name for most of the public variables created in the class, as they contain a dash. Is there a trick to this?
Correct your json look at @NeonBoxx answer
The json string I receive from the service is correct, but forgot to put an opening bracket when writing the question.
So now that you have your class you can start populate your DataTable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.