Can Json save reference only

Junfeng Liang 156 Reputation points
2021-11-28T08:27:06.327+00:00

I am trying to save multiple lists of objects. The classes of these object is like this:

class Company

{

  string Name;

  List<Person> Employees= new List<Person>()

//...

}

The Person is another class:

class Person

{

int ID;

string Name;

//....

}

I found that even I use "JsonSerializerOptions { IncludeFields = true, ReferenceHandler = ReferenceHandler.Preserve, WriteIndented = true }); " as someone taught me, in the string that save the list of companies, I still have the information of the employees (ID, Name...). I want to save the information of all the companies in one file and all the information of person in another file. Is it possible?

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jack J Jun 25,316 Reputation points
    2021-11-29T02:53:36.987+00:00

    @Junfeng Liang , you could try to use Json.Net to delete the specific element to generate two different json files.

    Here is a code example you could refer to.

    class Program  
        {  
            static void Main(string[] args)  
            {  
                Company company = new Company();  
                company.Name = "test1";  
                company.Employees = new List<Person>() { new Person() { ID = 1001, Name = "emp1" }, new Person() { ID = 1002, Name = "emp3" }, new Person() { ID = 1003, Name = "emp3" } };  
                string jsonString = JsonConvert.SerializeObject(company);  
                JObject jo = JObject.Parse(jsonString);  
                jo.Property("Employees").Remove();  
                string json1 = jo.ToString();  
                File.WriteAllText("test1.json", json1);  
                jo = JObject.Parse(jsonString);  
                jo.Property("Name").Remove();  
                string json2 = jo.ToString();  
                File.WriteAllText("test2.json", json2);  
      
      
            }  
        }  
      
      
        class Company  
      
        {  
      
            public string Name { get; set; }  
      
            public List<Person> Employees { get; set; }  
      
      
        }  
      
      
        class Person  
      
        {  
      
            public int ID { get; set; }  
      
            public string Name { get; set; }  
      
      
        }  
    

    Result:

    153138-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

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