0

I want to deserialize a JSON into a DataSet setting the columns types according to a custom schema

static void JsonToDataSet(ref DataSet pDs, string json, string schema)
{
    pDs = JsonConvert.DeserializeObject<DataSet>(json);
    DataSet teste = JsonConvert.DeserializeObject<DataSet>(schema);
    foreach(DataRow row in teste.Tables[0].Rows)
    {
        switch (row["type"].ToString())
        {
            case "int":
                pDs.Tables[0].Columns[row["field"].ToString()].DataType = typeof(Int32);
                break;
        }
    }
}

How can i change the datatype of the columns after filling the DataSet?

json

{
   "dados":[
      {
         "cd_ordem_servico":64773,
         "cd_solicitante":24379,
         "cd_tecnico":"24638",
         "cd_responsavel":24379,
         "nm_tecnico":"Giovane \"O Inocente\" BDT",
         "cliente":null,
         "nm_cidade":null,
         "dt_inicio":null,
         "dt_fim":null,
         "tp_duracao":null,
         "tp_viagem":"0",
         "cd_status":"4",
         "cd_status_os":4,
         "qt_produtos":0,
         "qt_contatos":0,
         "fl_turma_criada":0,
         "cd_head":0,
         "cd_status_os_complementar":null,
         "cd_head_solicitante":0,
         "cd_gerente_solicitante":256,
         "fl_complementar":"0",
         "fl_data_hora":0,
         "fl_item_cht":1,
         "cd_gerente_tecnico":256,
         "sou_cs":0,
         "cd_criador":"24379",
         "ds_etapa":null,
         "nm_solicitante":"Émerson Ariel Schmitt BDT",
         "nm_estado":null,
         "fl_pedido":"Sim",
         "ds_local":null,
         "ds_envio_hardware":"Não",
         "fl_envio_hardware":0,
         "fl_enviado":null,
         "dt_envio_hardware":null,
         "ds_enviado":"N/A",
         "ds_status":null,
         "ds_marcadores":"",
         "cd_marcadores":"",
         "nm_transporte":null
      },
      {
         "cd_ordem_servico":64760,
         "cd_solicitante":17438,
         "cd_tecnico":"17438",
         "cd_responsavel":17438,
         "nm_tecnico":"Ágatha Mile de Almeida Rodrigues BDT",
         "cliente":" CLIENTE TESTE DSV",
         "nm_cidade":"Americana",
         "dt_inicio":"2021-06-30T08:00:00",
         "dt_fim":"2021-06-30T09:00:00",
         "tp_duracao":1.0,
         "tp_viagem":"0",
         "cd_status":"6",
         "cd_status_os":6,
         "qt_produtos":1,
         "qt_contatos":1,
         "fl_turma_criada":0,
         "cd_head":0,
         "cd_status_os_complementar":"1",
         "cd_head_solicitante":0,
         "cd_gerente_solicitante":256,
         "fl_complementar":"0",
         "fl_data_hora":1,
         "fl_item_cht":1,
         "cd_gerente_tecnico":256,
         "sou_cs":0,
         "cd_criador":"17438",
         "ds_etapa":null,
         "nm_solicitante":"Ágatha Mile de Almeida Rodrigues BDT",
         "nm_estado":"SP",
         "fl_pedido":"Não",
         "ds_local":"SKA (Americana/SP)",
         "ds_envio_hardware":"Não",
         "fl_envio_hardware":0,
         "fl_enviado":null,
         "dt_envio_hardware":null,
         "ds_enviado":"N/A",
         "ds_status":"Não se aplica",
         "ds_marcadores":"",
         "cd_marcadores":"",
         "nm_transporte":"Avião"
      }
   ]
}

schema

{
   schema:[
      {
         field:"cd_ordem_servico",
         type:"int"
      },
      {
         field:"cd_solicitante",
         type:"int"
      },
      {
         field:"cd_tecnico",
         type:"int"
      },
      {
         field:"cd_responsavel",
         type:"int"
      },
      {
         field:"nm_tecnico",
         type:"string"
      },
      {
         field:"cliente",
         type:"string"
      },
      {
         field:"nm_cidade",
         type:"string"
      },
      {
         field:"dt_inicio",
         type:"date"
      },
      {
         field:"dt_fim",
         type:"date"
      },
      {
         field:"tp_duracao",
         type:"int"
      },
      {
         field:"tp_viagem",
         type:"int"
      },
      {
         field:"cd_status",
         type:"int"
      },
      {
         field:"cd_status_os",
         type:"int"
      },
      {
         field:"qt_produtos",
         type:"int"
      },
      {
         field:"qt_contatos",
         type:"int"
      },
      {
         field:"fl_turma_criada",
         type:"bool"
      },
      {
         field:"cd_head",
         type:"int"
      },
      {
         field:"cd_status_os_complementar",
         type:"int"
      },
      {
         field:"cd_head_solicitante",
         type:"int"
      },
      {
         field:"cd_gerente_solicitante",
         type:"int"
      },
      {
         field:"fl_complementar",
         type:"bool"
      },
      {
         field:"fl_data_hora",
         type:"bool"
      },
      {
         field:"fl_item_cht",
         type:"bool"
      },
      {
         field:"cd_gerente_tecnico",
         type:"int"
      },
      {
         field:"sou_cs",
         type:""
      },
      {
         field:"cd_criador",
         type:"int"
      },
      {
         field:"ds_etapa",
         type:"string"
      },
      {
         field:"nm_solicitante",
         type:"string"
      },
      {
         field:"nm_estado",
         type:"string"
      },
      {
         field:"fl_pedido",
         type:"bool"
      },
      {
         field:"ds_local",
         type:"string"
      },
      {
         field:"ds_envio_hardware",
         type:"string"
      },
      {
         field:"fl_envio_hardware",
         type:"bool"
      },
      {
         field:"fl_enviado",
         type:"bool"
      },
      {
         field:"dt_envio_hardware",
         type:"date"
      },
      {
         field:"ds_enviado",
         type:"string"
      },
      {
         field:"ds_status",
         type:"string"
      },
      {
         field:"ds_marcadores",
         type:"string"
      },
      {
         field:"cd_marcadores",
         type:"int"
      },
      {
         field:"nm_transporte",
         type:"string"
      }
   ]
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 1
    What is wrong with the shared code snippet ? – Jamshaid K. Oct 05 '21 at 20:52
  • 1
    Does `ConvertColumnType(this DataTable dt, string columnName, Type newType)` from [this answer](https://stackoverflow.com/a/44683870/3744182) by [Marc Ferrold](https://stackoverflow.com/users/8195996/marc-ferrold) to [How To Change DataType of a DataColumn in a DataTable?](https://stackoverflow.com/q/9028029/3744182) answer your question? If not, what additional help do you need? – dbc Oct 05 '21 at 23:00

0 Answers0