👍 Opening and adding text to a json file

Posts 1–15 of 15 · Page 1 of 1
👍 Opening and adding text to a json file
Hello! I needed help opening and added text to a json file, as you know, json files are not regular text files, so I need some way to find a spot inside of the json file, and add the code according to the file structure, please help!
I also had this problem when I needed to make a minecraft server on windows 8. I had no problems on my mac since it opened as a text file, but here's what I figured out and worked for me:
1. Make the .json file a text file by adding .txt at the end of it. (rename)
2. Open it using notepad or whatever textedit program you have.
3. Do all changes you want.
4. Save your changes and close the program.
5. Make it back to json by adding .json (rename once again)
6. Done!
You should be able to read/write to it normally like any type of text file.
Quote Originally Posted by Mayion View Post
You should be able to read/write to it normally like any type of text file.
Are you sure bro?

Quote Originally Posted by Jorndel View Post


You could try and IO.File.ReadAllLines()
And store that to an string array, if you know the line you want to add or edit some text you can just use:

MyStringArray(5) = "This is my line!"

This would edit the text on line 5. OFC you need to write it back

IO.File.WriteAllLines(MyStringArray)
You let laugh, go away please.

@America use a stream or serialize and deserialize it
Quote Originally Posted by Raydenman View Post
Are you sure bro?



You let laugh, go away please.

@America use a stream or serialize and deserialize it

k
Quote Originally Posted by Mayion View Post



k
Ok bro you understood all.
Ok. [sarcasm]
Quote Originally Posted by America View Post
Hello! I needed help opening and added text to a json file, as you know, json files are not regular text files, so I need some way to find a spot inside of the json file, and add the code according to the file structure, please help!
You could try and IO.File.ReadAllLines()
And store that to an string array, if you know the line you want to add or edit some text you can just use:

MyStringArray(5) = "This is my line!"

This would edit the text on line 5. OFC you need to write it back

IO.File.WriteAllLines(MyStringArray)
That is very useful info, thank you so much, I will defintly try that way.
Quote Originally Posted by America View Post
That is very useful info, thank you so much, I will defintly try that way.
Don't read that, you must serialize/deserialize a JSON file, btw is it hosted?
@Mayion
Quote Originally Posted by Raydenman View Post
Maybe you didn't understand you are useless.
You're not even the one asking for help.
Atleast I gave code examples and informed the user about one possible way. No need to be rude like this, no one likes that
Statically typed languages such as VB.NET aren't particularly well-suited for dealing with dynamic data structures like JSON. If you have the option you'd be better off using a dynamically-typed language such as Python, PHP...etc. as they have robust built-in ways of dealing with JSON data.

However, you do have options with VB.NET. If you're targetting .NET Framework 4.0 (not Client Profile) then you can add a reference "System.Web.Extensions" in your project and utilize the JavaScriptSerializer class.

A really basic example:
Code:
Imports System.Web.Script.Serialization

' ...snip

        Dim json As String = IO.File.ReadAllText("B:\test.json")
        Dim serializer As New JavaScriptSerializer()

        Dim jsonObj = serializer.DeserializeObject(json)
        ' You can now use the array-access notation to extract values from the json Object
        MessageBox.Show(jsonObj("example").ToString())

        ' To add new values you can again use array notation:
        jsonObj("new-key") = "Some Value"

        ' And write it back out to disk:
        IO.File.WriteAllText("B:\test.json", serializer.Serialize(jsonObj))

Before I can give you much more info though, what is the format of your JSON file? Is it going to be the same format every time or does it change a lot? If you post a copy of it here I can give you a more concrete example of how to proceed.
Here is the format

Code:
{
  "profiles": {
    "PROFILENAMEHERE": {
      "name": "PROFILENAME"
    }
  },
  "selectedProfile": "PROFILENAME",
  "clientToken": "clientTokenHere",
  "authenticationDatabase": {
    "authenticationDatabase Here": {
      "displayName": "DynamicGaming1",
      "userProperties": [
        {
          "name": "twitch_access_token",
          "value": "twitchaccesstokenHere"
        }
      ],
      "accessToken": "Access Token",
      "userid": "userIDhere",
      "uuid": "uuidHere",
      "username": "Usersnames email address here"
    }
  },
  "selectedUser": "selectedUser (token probably, I suck at coding)",
  "launcherVersion": {
    "name": "1.6.11",
    "format": 17
  }
}
I am a huge noob, so please don't be harsh.
Quote Originally Posted by America View Post
Here is the format
What are you trying to do?
Quote Originally Posted by Mayion View Post

What are you trying to do?
You see, I have a program that installs hack clients automatically for minecraft, but it is very bland and simple, not to useful to many. So what I wanted to do, is when you click install, it installs the client, then it somehow opens a json file, and adds all the properties accordingly to the json structure. I don't know java so it is very confusing to me.
Posts 1–15 of 15 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us