3

Tried this with JSON.NET 6.0

DataSet ds = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>
("{\"tab1\":[{\"col1\":\"val1\"}]}"); // OK

DataTable dt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>
("{\"col1\":\"val1\"}"); // System.OutOfMemoryException

why?

thank you

3 Answers 3

1

IMO it's a bug, But if you want to deserialize DataSet or DataTable this might help you.

Sign up to request clarification or add additional context in comments.

Comments

1

The JSON in your second example represents a only a single row of data, not a DataTable. A DataTable is an ordered collection of DataRows, therefore it needs to have square brackets in the JSON. Try it like this instead:

string json = "[{\"col1\":\"val1\"}]";
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);

I am not sure why you are getting an OutOfMemoryException; I get a JsonSerializationException when I try it, as I would expect. Perhaps this was a bug that was fixed in the most recent version of Json.Net.

Comments

0

I have over come with this issue by creating JsonSerializer object. My code is as given below.

using (StreamReader r = new StreamReader(yourfilePath))
{
    using (JsonReader reader = new JsonTextReader(r))
    {
         JsonSerializer serializer = new JsonSerializer();
         T lstObjects =  serializer.Deserialize<T>(reader);
    }
}

Here yourfilePath :- is your file current full path T :- is your object name, whether it could be Dataset, DataTable or any custom Object.

Comments

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.