Brian Muscat
Brian Muscat

Reputation: 5

Adding items to List after reading from JSON

I am have json file from which I am reading the data in and inserting into a list. With the following Code I read from the json file and insert it into the list.

public class prepackList
{
    public int prePackID;
    public int stockID;
    public string stockItem;
    public int reservedUpcID;
    public string reservedUpc;
    public string weight;
    public DateTime packedDate;
    public DateTime bestBefore;
    public float priceONPackage;}


    StreamReader reader = new StreamReader(System.Web.Hosting.HostingEnvironment.MapPath("~/jsonData/prepackJSON.json"));
    string jsonData = reader.ReadToEnd();
    List<prepackList> listItemsJson = JsonConvert.DeserializeObject<List<prepackList>>(jsonData)

}

Now the problem is I need to append to this list so then I can seriaze it agin into a json file. Can anyone help?

Upvotes: 0

Views: 53

Answers (1)

RagtimeWilly
RagtimeWilly

Reputation: 5445

Unless I'm missing something you can just add the item as normal and serialize again:

listItemsJson.Add(new prepackList());

var updatedJson = JsonConvert.SerializeObject(listItemsJson);

Upvotes: 1

Related Questions