Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new in Json, already searching many info but no luck, based on:
json add new object to existing json file C# - Stack Overflow[^] (have tried it, and it's works)
But that needs an array json format (begin with [ , ends with ]), mine is object json format (begin with { , ends with }).
So, how to do that in Newtonsoft.Json without change the json into Json array format?



my Json (object):
{
  "Group": "Group1",
  "ID": 1,
  "Name": "Author1",
  "Description": "Desc1"
}


Using this to create it:
class Author
{
  public string Group { get; set; }
  public int ID { get; set; }
  public string Name { get; set; }
  public string Description { get; set; }
}

private Author NewAuthor()
{
  Author author = new Author()
  {
  Group = "Group1",
  ID = 1,
  Name = "Author1",
  Description = "Desc1"
  };
  return author;
}

private void Btn_Create_Click(object sender, EventArgs e)
{
  string jsonData = JsonConvert.SerializeObject(NewAuthor(), Formatting.Indented);
  File.WriteAllText(Filepath, jsonData);
}



Desired output:
{
  "Group": "Group1",
  "ID": 1,
  "Name": "Author1",
  "Description": "Desc1"
},
{
  "Group": "Group2",
  "ID": 2,
  "Name": "Author2",
  "Description": "Desc2"
}


What I have tried:

Based on json add new object to existing json file C# - Stack Overflow[^]
below code works, but when the json begin with [ not {

Author author = NewAuthor();
var lst = JsonConvert.DeserializeObject<List<Author>>(json) ?? new List<Author>();
lst.Add(author);

// Write to file
string updatedJson = JsonConvert.SerializeObject(lst, Formatting.Indented);
File.WriteAllText(filepath, updatedJson);
Posted
Updated 6-Nov-21 4:26am
v4

 
Share this answer
 
Comments
DevJr 6-Nov-21 6:11am    
thanks sir, have been around there but not found the way.
Richard MacCutchan 6-Nov-21 6:22am    
You need to understand that unless you learn the subject(s) properly, and understand how it works, then you are wasting your time. Follow the links I, and OriginalGriff, gave you and actually study the subject. You will not find the code for your specific question just by using Google.
Create an object that contains the two instances as properties and serialize that to JSON.

Seriously, you have three questions in thirty minutes here that show you have no idea what JSON is or what you should do with it - you appear to be guessing and hoping instead of actually learning what you need to know to manipulate the data at all ...
Start here: JSON Tutorial[^] and then move on to here: Json.NET Documentation - Introduction[^]
 
Share this answer
 
Comments
DevJr 6-Nov-21 5:41am    
Thanks for your advice sir, really appreciate it. Sorry for many questions, i thought this was a correct place to ask?, any assist for actual C# code for how to implement it would be super cool :)
OriginalGriff 6-Nov-21 6:22am    
It is, but we are here to help those who are stuck, not do it all for you! And you don't seem to have done anything for yourself yet ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900