-2

I have List of companies and im trying to add a new company to a json file but It either doesn't go into the right place or it overwrites everything in that section. Im using Json .net to do this

I have this and Im trying to add a company to it

"Companies": []

Ive tried doing this but it just overwrites what is already in the "Companies" area.

Company company = new Company
            {
                CompanyName = textBox1.Text,
                IPO = Convert.ToDouble(textBox2.Text),
                Category = CategorycomboBox1.SelectedItem.ToString(),
                Description = textBox4.Text,
                StartDate = Convert.ToInt32(textBox5.Text)
            };

            AddProductListItemsToFinishedJSON(company);
            AddNewsArticlesListItemsToFinishedJSON(company);

            JObject jo = JObject.FromObject(company);
            string NewCompanyJson = jo.ToString();

            string FileWritingToJson = File.ReadAllText(path);

            var NewCompanyJsonParsed = JObject.Parse(NewCompanyJson);
            var FileWritingToJsonParsed = JObject.Parse(FileWritingToJson);


            Debug.WriteLine(FileWritingToJson);
            Debug.WriteLine(NewCompanyJson);

            FileWritingToJsonParsed["Companies"] = NewCompanyJsonParsed;

            Debug.WriteLine("-----------------------");
            Debug.WriteLine(FileWritingToJsonParsed);

            File.WriteAllText(@"D:\COMPTESTTES.json", FileWritingToJsonParsed.ToString());

            SaveJSONFile(company);

It either overwrites that section or adds it to the bottom of the file.

Jack Soder
  • 77
  • 1
  • 7
  • If the duplicate I selected is not enough please double-check search results you've tried (like https://www.bing.com/search?q=c%23+append+json+file) and [edit] to clarify why existing solutions did not work so new suggestions could be provided. – Alexei Levenkov Sep 12 '19 at 22:15
  • @AlexeiLevenkov that doesnt show how to insert it into a specific area. – Jack Soder Sep 13 '19 at 02:33
  • Jack Soder - it is very hard to see what exactly you have problem with based on information in the question. So far I believe duplicate addresses the problem as other readers would see it - which is the goal of SO posts - help future visitors with answers. If you feel that duplicate does not address the question - make sure to [edit] it to explain what help you are looking for. Also I'd strongly recommend re-reading answer by Dai and answers in the duplicate - to see if working on strongly typed values make your code simpler. – Alexei Levenkov Sep 13 '19 at 20:33
  • If you want to stick with JObjects see if someone had similar questions before like https://www.bing.com/search?q=c%23+json+insert+jobject - this will help you with your [edit] or may even answer your question. – Alexei Levenkov Sep 13 '19 at 20:34

1 Answers1

-1

I'd avoid using the low-level JObject API and use JsonConvert instead. You will need to define a class that represents the schema of the file:

class CompaniesFile
{
    public List<Company> Companies { get; set; }
}

class Company
{
    public String CompanyName { get; set; }
    public String IPO { get; set; }
    public String Category { get; set; }
    public String Description { get; set; }
    public Int32 StartDate { get; set; }
}

Then like so:

String existingFileContents = File.ReadAllText( path );

CompaniesFile json = JsonConvert.DeserializeObject<CompaniesFile>( existingFileContents );

Company newCompany = new Company
{
    CompanyName = textBox1.Text,
    IPO = Convert.ToDouble(textBox2.Text),
    Category = CategorycomboBox1.SelectedItem.ToString(),
    Description = textBox4.Text,
    StartDate = Convert.ToInt32(textBox5.Text)
};

json.Companies.Add( newCompany );

String newFileContents = JsonConvert.Serialize( json );
File.WriteAllText( newFileContents );
Dai
  • 141,631
  • 28
  • 261
  • 374