1

I'm trying to get a range of json values into a C# DataTable. I'm at the point where Visual Studio recognises the json objects, but I'm getting this error:

Newtonsoft.Json.JsonSerializationException: 'Additional text found in JSON string after finishing deserializing object.'

Here is an example of the json array:

{
  "aaData": [
    [
      1,
      "Stop",
      "14/03 15:30:03",
      "14/03 15:30:58",
      0,
      "Address",
      "Lat, Long",
      184,
      0,
      9,
      "False"
    ],
    [
      1,
      "Stop",
      "14/03 15:30:03",
      "14/03 15:30:58",
      0,
      "Address",
      "Lat, Long",
      184,
      0,
      9,
      "False"
    ]
  ]
}

And I'm using this code:

JToken token = JObject.Parse(responsetext);
string apistatus = token.SelectToken("aaData").ToString();
DataSet table = JsonConvert.DeserializeObject<DataSet>(apistatus);

But it is at that last line that the exception is thrown.

If I print the string apistatus to the console I can see it has got the arrays - and even when using the JSON Visualiser in VS the arrays are being recognised - but not sure what is wrong. Here's the string apistatus in the console:

   [
    [
      1,
      "Stop",
      "14/03 15:30:03",
      "14/03 15:30:58",
      0,
      "Address",
      "Lat, Long",
      184,
      0,
      9,
      "False"
    ],
    [
      1,
      "Stop",
      "14/03 15:30:03",
      "14/03 15:30:58",
      0,
      "Address",
      "Lat, Long",
      184,
      0,
      9,
      "False"
    ]
  ]

What am I doing wrong? Or is there a much better way to get this particular json array of values into a DataTable?

3

1 Answer 1

0

I am not sure about what you are trying to do. If you want to parse a json, you can directly use DeserializeObject:

Demo

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Data
{
    public List<List<object>> AaData { get; set; }
}    

public class Program
{
    public static void Main()
    {
        var json = ""; // your json here
        var m = JsonConvert.DeserializeObject<Data>(json);
        Console.WriteLine(m.AaData[0][0]);
        Console.WriteLine(m.AaData[1][2]);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes that is almost it.That deserialised JSON needs to go into a DataTable now but not sure how...?
Any thoughts on how I can do that @aloisdg?

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.