String size exceeds the value set in the maxJsonLength property

Asked

Viewed 2,568 times

1

I’m trying to generate a file Json from a SELECT of the SQL SERVER 2016 table. There are about 4713 rows, this is exceeds the value set in the property maxJsonLength. I can exceed that set value?

Web.config

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="500000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>  

Class Model:

public class ProcessoMobile
        {
            public string Natureza { get; set; }
            public string Vara { get; set; }
            public string Agencia { get; set; }
            public string Comarca { get; set; }
            public Decimal Valor { get; set; }
            public string GCPJ { get; set; }
            public string ParteContraria { get; set; }
            public string AdvogadoContraria { get; set; }
            public string Telefone { get; set; }
            public DateTime DataDistribuicao { get; set; }
            public string Carteira { get; set; }
            public string Contrato { get; set; }
            public string CidadeAgencia { get; set; }
            public string ProcessoID { get; set; }
            public bool PesquisaBens { get; set; }
            public bool Ativo { get; set; }
            public bool Arquivado { get; set; }
            public bool Irrecuperavel { get; set; }
            public bool Encerrado { get; set; }
            public string CarteiraAtual { get; set; }
            public string ContratoAtual { get; set; }
            public string CpfCnpj { get; set; }
            public string Conta { get; set; }
            public string NomeAdvogado { get; set; }
        }

Function:

public static void CriaJson()
{
    List<ProcessoMobile> processList = new List<ProcessoMobile>();

    using (SqlConnection con = new SqlConnection(MyConnectionString))
    {
        using (SqlCommand command = new SqlCommand("select_Processo_Mobile", con))
        {
            command.CommandType = CommandType.StoredProcedure;

            con.Open();

            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                ProcessoMobile p = new ProcessoMobile()
                {

                    Natureza = reader["Natureza"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Natureza"]),
                    Vara = reader["Vara"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Vara"]),
                    Agencia = reader["Agencia"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Agencia"]),
                    Comarca = reader["Comarca"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Comarca"]),
                    Valor = reader["Valor"] ==
                    DBNull.Value ? decimal.MinValue :
                    Convert.ToDecimal(reader["Valor"]),
                    GCPJ = reader["GCPJ"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["GCPJ"]),
                    ParteContraria = reader["ParteContraria"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ParteContraria"]),
                    AdvogadoContraria = reader["AdvogadoContraria"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["AdvogadoContraria"]),
                    Telefone = reader["Telefone"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Telefone"]),
                    DataDistribuicao = reader["DataDistribuicao"] ==
                    DBNull.Value ? DateTime.MinValue :
                    Convert.ToDateTime(reader["DataDistribuicao"]),
                    Carteira = reader["Carteira"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Carteira"]),
                    Contrato = reader["Contrato"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Contrato"]),
                    CidadeAgencia = reader["CidadeAgencia"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CidadeAgencia"]),
                    ProcessoID = reader["ProcessoID"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ProcessoID"]),
                    PesquisaBens = reader["PesquisaBens"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["PesquisaBens"]),
                    Ativo = reader["Ativo"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Ativo"]),
                    Arquivado = reader["Arquivado"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Arquivado"]),
                    Irrecuperavel = reader["Irrecuperavel"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Irrecuperavel"]),
                    Encerrado = reader["Encerrado"] ==
                    DBNull.Value ? false :
                    Convert.ToBoolean(reader["Encerrado"]),
                    CarteiraAtual = reader["CarteiraAtual"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CarteiraAtual"]),
                    ContratoAtual = reader["ContratoAtual"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["ContratoAtual"]),
                    CpfCnpj = reader["CpfCnpj"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["CpfCnpj"]),
                    Conta = reader["Conta"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["Conta"]),
                    NomeAdvogado = reader["NomeAdvogado"] ==
                    DBNull.Value ? string.Empty :
                    Convert.ToString(reader["NomeAdvogado"]),

                };
                processList.Add(p);
            }
            reader.Close();
            reader.Dispose();
            con.Close();
            con.Dispose();
            command.Dispose();

            var jsonSerialiser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString = jsonSerialiser.Serialize(processList);
            System.IO.File.WriteAllText(@"D:\processList.json", jsonString);
        }
    }
}

inserir a descrição da imagem aqui

1 answer

2


Usually, so much for Serialization when Deserialize I use the framework Newtonsoft of namespace Newtonsoft.Json which, most of the time, is already incorporated into the project. However, if it does not come, it is possible to install it by Nuget

PM> Install-Package Newtonsoft.Json 

Anyway, let’s go to the code.

I don’t know the size of the data coming from your database, but I created a class equivalent to yours and assigned all of them the text "Serializeobject test with Newtonsoft.Json".

No big problems, I managed to make the Serialization of 500000 lines.

follows how to use it

public static void CriaJson()
{
    List<ProcessoMobile> processList = new List<ProcessoMobile>();

    //Código para obter os dados

    string jsonString =  JsonConvert.SerializeObject(processList);
    System.IO.File.WriteAllText(@"D:\processList.json", jsonString);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.