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?

reviloRetals
  • 485
  • 1
  • 7
  • 14

1 Answers1

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]);
    }
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105