3 Replies - 722 Views - Last Post: 28 October 2014 - 05:42 AM Rate Topic: -----

#1 Inuinu172   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

[Problem] Read, Parse and Update JSON from/to file

Posted 27 October 2014 - 02:59 PM

Hey, I'm quite new to C#, I only started learning two days ago, So I'm really clueless about a lot of things.
There is some code I've been working on that handles user information by storing it as JSON and then writing it to the file.
This JSON is also accessed later and updated before written back to the file.
I've got the writing part sorted, But I need some help with reading, getting information from the JSON and then updating a certain value before writing it to the file again.

This is what I have so far:

MainFile.cs
        public string userData()
        {
            // Reading JSON
            using (StreamReader r = new StreamReader("data/users.txt"))
            {
                string jsonOld = r.ReadToEnd();
                Console.WriteLine(jsonOld);
            }

            
            // ADDING A USER
            List<User> _data = new List<User>();
            _data.Add(new User()
            {
                Name = "Inuinu172",
                ID = "111111111",
                trades = 0,
                coins = 0
            });
            string json = JsonConvert.SerializeObject(_data.ToArray());

            //write user to file
            System.IO.File.WriteAllText(@"data/users.txt", json);
            // This overwrites whatever is in the file already
            return;
        }




User.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MainFile
{
    class User
    {
        public string Name = "";
        public string ID = "";
        public double coins = 0;
        public int trades = 0;
    }
}




Edit: I don't want you to do everything for me, Just give me a basic idea on how to do the following

I'm wanting something that would allow me to type

addUser(name, id, coins, trades);

And the script would read the json from the file, Check if it already exists, if it exists, do nothing.
If the user doesn't already exist in the json, it would add them before saving the json to the file again.

The second thing I'm trying to do is something like this:

updateUserData(name, id,coins,trades);

And the script would read the json from the file, Check if it already exists, if it exists, update all the values with the new ones before writing it back to the file again.

Third and final thing I'm trying to do is gather that information and store it in a variable so I can use in a different part of the script.

getUserData(id);

The script would read and search the JSON for a user matching that ID, When it's found, it would get the Name, Coins and Trades and set them to variables.

I have json.net installed, if that's important to know. Edit: Yes, I've read the documentation
I've been straining my brain for a few hours trying to figure this one out.
Google hasn't helped too much, so I came here to ask if you all would know how to achieve all of this.

Massive thanks in advance,
Husky

This post has been edited by Inuinu172: 28 October 2014 - 03:18 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [Problem] Read, Parse and Update JSON from/to file

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6825
  • View blog
  • Posts: 23,203
  • Joined: 05-May 12

Re: [Problem] Read, Parse and Update JSON from/to file

Posted 27 October 2014 - 08:31 PM

Did you even try reading the Json.NET documentation? I managed to find this with 30 seconds of looking through the table of contents:

Serializing Collections
Was This Post Helpful? 0
  • +
  • -

#3 Inuinu172   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 17-July 13

Re: [Problem] Read, Parse and Update JSON from/to file

Posted 28 October 2014 - 03:08 AM

View PostSkydiver, on 27 October 2014 - 08:31 PM, said:

Did you even try reading the Json.NET documentation? I managed to find this with 30 seconds of looking through the table of contents:

Serializing Collections


Hey, Thanks for the reply.
Glad you were able to do this in "30 seconds", But I'm afraid you failed to read my above post where I stated that I'm very new to C#, I only started learning it a while ago, As simple as it may seem to you, It's rather complicated in my eyes.
I read the documentation but hardly understood it at all, That's why I came here.
Under the logo it has "More than just answers - Community learning."
I was hoping that some of the community could help me learn, but it doesn't seem like it.
Thanks anyway, I guess I'll take another look at the documentation.

This post has been edited by Inuinu172: 28 October 2014 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6825
  • View blog
  • Posts: 23,203
  • Joined: 05-May 12

Re: [Problem] Read, Parse and Update JSON from/to file

Posted 28 October 2014 - 05:42 AM

If you are very new to C#, you should not be playing with JSON for serialization. In fact, you should not be tackling serialization and deserialization at all until you get a better grasp of the language and its concepts.

C# and the .NET Framework more naturally serializes and deserializes to XML because of Microsoft's big bets on XML early on. It was only later that .NET Framework started supporting JSON. It's why JSON.NET got created because during the time that Microsoft came around to accept that JSON was going to be the preferred format by web developers, somebody had to fill the void.

Anyway that sample code in the documentation is all you really need. For your addUser(), you'll need to load the current collection from the file, add another entry into the collection, and save the collection back out. See how in line 15 and 16 in the serialization sample how new items were added into the collection? You'll need to the same thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1