Json SerializeObject

CSharp · Console

/Program.cs

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
		string json = "[{\"Id\":0,\"Nome\":\"\",\"Endereco\":\"\"},{\"Id\":1,\"Nome\":\"\",\"Endereco\":\"\"}]";
		List<JsonResult> items = JsonConvert.DeserializeObject<List<JsonResult>>(json);
		
		var last = items[items.Count - 1].Id + 1;
		
		items.Add(new JsonResult()
		{
			Id = last,
			Nome = "Leonardo",
			Endereco = "Rua de baixo"
		});
		
		string myJsonOutput = JsonConvert.SerializeObject(items);
		Console.WriteLine(myJsonOutput);
		
	}
	
	public class JsonResult
	{
		public int Id { get; set; }
		public string Nome { get; set; }
		public string Endereco { get; set; }
	}
}
;