0

I am try to create some sort of register for a gas station. Its simple enough where i just keep some files with worker info, gas info, and receipts. However i am having trouble with storing/then reading receipt data in my receipt JSON file. Here is my code for storing data to a file:

var newReceipt = new List<Receipts>
       {
                new Receipts
                {
                //...
                //data
                //...
                }
       };
var nReceipt = JsonConvert.SerializeObject(newReceipt);
File.AppendAllText(receiptFile, nReceipt);

Using this code my file looks like this after 2 runs of it

[
  {
    "pricePerL": 10.22,
    "ID": "NH1164DTDE",
    "amount": 10.0,
    "tax": 25.5500011,
    "totalPrice": 102.200005,
    "time": "01.20.2023 13:12",
    "sort": "beznin",
    "typeOfPayment": "card",
    "employee": "Denis"
  }
][
  {
    "ID": "300FIMTU7U",
    "sort": "disel",
    "employee": "Denis",
    "amount": 150.0,
    "pricePerL": 13.24,
    "tax": 496.5,
    "totalPrice": 1986.0,
    "typeOfPayment": "cash",
    "time": "01.20.2023 13:19"
  }
]

I have trouble reading this using my other function due to square brackets between. Other function simply uses foreach to go through the file and writes out some info about receipts and i found it working when there are no square brackets between two receipts.

I have tried a bunch of way to record data to that JSON file but there are always those square brackets there. My other function for writing out all receipts in that file returns exception: "Additional text encountered after finished reading JSON content "

My other function:

static void pregledRacuna(List<Receipts> racuniLista)
        {
            try
            {
                foreach (Receipts r in racuniLista)
                {
                  //writes out id and date
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

P.S. this is my first post here so if there is any clarification needed please do say so!

4
  • 1
    please read json.org to understand what that json is and what you can expect. simple appending text is not really json-like and you have to know what you are doing and what kind of json-library you are using (that may support that simple appending) Commented Jan 20, 2023 at 12:54
  • [ {receipt} ][ {receipt} ] means that you have two (or in the actual file probably more) collections of your object in a row. What is Receipts ? From the name I assume this is a collection of Receipt? That would explain the json result. You should just serialize List<Receipt> (no 'S') or Receipts directly Commented Jan 20, 2023 at 12:55
  • Unfortunately, like the other commenters have stated, you cannot simply modify JSON by appending new data as it creates invalid JSON. You will have to read the existing data, modify it in your program and then write out the complete updated data. Commented Jan 20, 2023 at 12:58
  • Don't just append data to the file. Read in the entire file to an object, add your data to that object, write the entire object back out to a file. (Or switch to using a database as data grows.) Commented Jan 20, 2023 at 12:58

1 Answer 1

2

you can not just append a new text to the file, you will have to read all text from the file, deserialize, add new item and write all after this

string json = File.ReadAllText(receiptFile);
var receipts= JsonConvert.DeserializeObject<List<Receipts>>(json);
receipts.Add(
                new Receipts
                {
                //...
                //data
                //...
                }
       );

json  = JsonConvert.SerializeObject(receipts);
File.WriteAllText(receiptFile, json);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.