Convert complex JSON to DataTable

4

Description:

I am developing a web solution in so that, from the front, put together an object JSON complex so that, when received in the back, save the values in a SQL Server 2012 database.

The JSON complex object is composed as follows:

[{
    "__KEY__": "73c14f5b-cbb9-8711-217a-a1a5453b7159",
    "sUserID": [80417452, 80426837, 79945118],
    "FechaInicio": "2017-04-19T05:00:00.000Z",
    "FechaFin": "2017-04-20T05:00:00.000Z",
    "Id_Franja": 3
}]

By using the following lines to serialize the JSON object and convert it to DataTable I get this result:

Code:

// La variable "myJson" contiene el JSON complejo.
DataTable jsonDataTable = (DataTable)JsonConvert.DeserializeObject(myJson, (typeof(DataTable)));

Result of serialization:

I must convert this complex JSON to a DataTable in C #; in this case, I need the values in the column "sUserID" to be in the DataTable as a string separated by commas.

How to convert this complex JSON object into a DataTable?

I've tried these solutions , but I can not adjust the code to achieve this goal.

I am looking for other options to convert complex JSON objects "like the example" in DataTable.

    
asked by Mauricio Arias Olave 27.04.2017 в 18:50
source

1 answer

3

The sUserID element is displayed in this way since the same compiler determines the type and detects that it is a array of int64[] , and since it can not display the content, it only shows the data type.

If that value could come as "sUserID": "[80417452, 80426837, 79945118]" , then you can determine it as a string .

Now, before deserializing it directly to a DataTable , it is better to convert it to a List<T> , so that from that list we manage it as the data source.

Class definition:

public class jsonContent
{
    public string __KEY__ { get; set; }
    public int[] sUserID { get; set; }
    public DateTime FechaInicio { get; set; }
    public DateTime FechaFin { get; set; }
    public int Id_Franja { get; set; }
    public string sUserIdNuevo
    {
        get
        {
            return String.Join("", new List<int>(this.sUserID).ConvertAll(i => i.ToString()).ToArray());
        }
    }
}

Note that since we can not convert sUserID directly to string we will rely on another variable called sUserIdNuevo and what it will do is concatenate the values of array of int64[] with String.Join("", new List<int>(this.sUserID).ConvertAll(i => i.ToString()).ToArray());

Given your comment that you only use the DataTable to go through it and use it as a variable for storage to the database, I suggest you go through the list and that would be enough:

List<jsonContent> jsonDataTable = (List<jsonContent>)JsonConvert.DeserializeObject(myJson, (typeof(List<jsonContent>)));

foreach (jsonContent item in jsonDataTable)
{
    //Aquí va toda la lógica de guardado a la base de datos
}

This article ( in English ) indicates 7 powerful reasons why change DataTable for generic collections.

    
answered by 27.04.2017 / 20:04
source