There’s no "nothing wrong"...
I ran a test using the same codebase as yours:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace TesteConsole
{
class Program
{
static void Main(string[] args)
{
var lista = new List<Teste>();
for (int i = 0; i < 300; i++)
lista.Add(new Teste{id = i, nome = "Teste "+ i});
var json = JsonConvert.SerializeObject(lista);
Console.WriteLine(json);
string result = string.Empty;
foreach (var item in lista)
{
result += JsonConvert.SerializeObject(item);
}
Console.WriteLine(result);
Console.ReadLine();
}
}
}
public class Teste
{
public int id { get; set; }
public string nome { get; set; }
}
Result of var json = JsonConvert.SerializeObject(lista)
:
[{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"},{"id":2,"nome":"Teste 2"},{"id":3,"nome":"Teste 3"},{"id":4,"nome":"Teste 4"}
[...]
{"id":297,"nome":"Teste 297"},{"id":298,"nome":"Teste 298"},{"id":299,"nome":"Teste 299"}]
Result of string result
:
{"id":0,"nome":"Teste 0"}{"id":1,"nome":"Teste 1"}{"id":2,"nome":"Teste 2"}{"id":3,"nome":"Teste 3"}{"id":4,"nome":"Teste 4"}
[...]
{"id":297,"nome":"Teste 297"}{"id":298,"nome":"Teste 298"}{"id":299,"nome":"Teste 299"}
I believe the problem lies only in the visualization and familiarization of the IDE... If you in debug mode "hovers the mouse" on top of the variable you want to see, an ellipsis will appear in case what you are inspecting has a larger content "than fits" in the inspection preview:
If you click "on the magnifying glass":
EDIT:
Note that in the case of how you are doing, the result is a json invalid, contrary to what is done with the serialization of the entire list: var json = JsonConvert.SerializeObject(lista);
Json INVALID:
{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"}
{"id":0,"nome":"Teste 0"}{"id":1,"nome":"Teste 1"}
Json VALID:
[{"id":0,"nome":"Teste 0"},{"id":1,"nome":"Teste 1"}]
Where are you viewing this output?
– Vinicius Zaramella
First, you must use
StringBuilder
, nor try to do as you are doing. Nothing indicates that what you are describing is even possible. Or you need to put more code that indicates this, or what happens depends on the data and not the code.– Maniero
Stringtextvisualizer, by clicking on the magnifying glass next to the variable.
– João Paulo Massa
Serialize Collection at once...?
– Marllon Nasser
Yes, the result is the same
– João Paulo Massa