how to inset a new jsondata in a text file

venki vicky 1 Reputation point
2022-11-28T08:24:06.103+00:00

i am working with uwp

try

        {  

            var path = @"text file\\GetAllEmp.txt";  
              

            string rawJson = File.ReadAllText(path, Encoding.UTF8);  

            ObservableCollection<EmployeeItem> Employee = new ObservableCollection<EmployeeItem>();  
            var jsonData = JsonConvert.SerializeObject(rawJson);  

            List<EmployeeItem> emp = JsonConvert.DeserializeObject<List<EmployeeItem>>(rawJson);  

            listitem.ItemsSource = emp;  

}

i need to insert a data Without deleting the existing data i have uploaded the file in solution explore

my text file contains
[
{
"Id": 5,
"GID": 1,
"SID": 34
},
{
"Id": 4,
"GID": 1,
"SID": 33
},
{
"Id": 3,
"GID": 1,
"SID": 32
}
]

public class EmployeeItem  
{  
  
[JsonProperty("Id")]  
public int Id { get; set; }  
  
[JsonProperty("GID")]  
public long GroupID { get; set; }  
  
[JsonProperty("SID")]  
public long SiteID { get; set; }  
}  

i have read my text file i just need to add a new data

i have given my text file in solution expolore
264743-text-file.png

is there is any other method please let me know

Developer technologies | Universal Windows Platform (UWP)
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-11-28T12:12:24.277+00:00

    Here is a simple example which I use Bogus library to mock-up data. The code was done in a console project but will work with any project type.

    Model

    public class Person  
    {  
        public int Id { get; set; }  
        public string FirstName { get; set; }  
        public string LastName { get; set; }  
        public DateTime BirthDate { get; set; }  
      
        public Person(int identifier)  
        {  
            Id = identifier;  
        }  
      
        public Person() { }  
      
        public string Details => $"{Id,-4}{FirstName} {LastName} {BirthDate:d}";  
    }  
    

    Create some data

    public class BogusOperations  
    {  
        public static List<Person> People(int count = 10)  
        {  
            int identifier = 1;  
      
            Faker<Person> fakePerson = new Faker<Person>()  
                    .CustomInstantiator(f => new Person(identifier++))  
                    .RuleFor(p => p.FirstName, f => f.Person.FirstName)  
                    .RuleFor(p => p.LastName, f => f.Person.LastName)  
                    .RuleFor(p => p.BirthDate, f => f.Date.Past(10))  
                ;  
      
      
            return fakePerson.Generate(count);  
      
        }  
    }  
    

    Read/write operations

    public class Operations  
    {  
        private static string FileName => "People.json";  
        public static JsonSerializerOptions Indented  
            => new() { WriteIndented = true };  
        public static List<Person> Read()  
        {  
            var list = JsonSerializer.Deserialize<List<Person>>(File.ReadAllText(FileName));  
      
            return list;  
        }  
        public static void Save(List<Person> list)  
        {  
            File.WriteAllText(FileName, JsonSerializer.Serialize(list, Indented));  
        }  
    }  
    

    Run it

    Operations.Save(BogusOperations.People(2));  
    var people = Operations.Read();  
    foreach (var person in people)  
    {  
        Console.WriteLine(person.Details);  
    }  
      
    people.Add(new Person()  
    {  
        Id = 3,   
        FirstName = "Jim",   
        LastName = "Smith",   
        BirthDate = new DateTime(1990,1,1)  
    });  
    Operations.Save(people);  
    Console.WriteLine();  
      
    people = Operations.Read();  
    foreach (var person in people)  
    {  
        Console.WriteLine(person.Details);  
    }  
    

    264748-f1.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.