Modify JSON database values with java in eclipse - java

I want to use eclipse and java to make a program that can parse json files in java only.
For example, I want to change a value in JSON to another value every second.
I know the jsonparse function in javascript, but to carry it over through scriptengine alone seems like a bad solution.
I also know that I can use Apache IO to read a file to string and then edit the string and read the file back, but it doesn't seem efficient.
Is there an effective way to modify values in a JSON.php file?
Here is a JSON.php file that I have.
[{"color": "red waves-effect waves-light", "value": "0", "activator": "null"}]
Here is a part of my java program that does the text editing.
String attentionVs = "" + attentionV;
File fileattentionV = new File("attentionV.php");
String fileContext2 = FileUtils.readFileToString(fileattentionV);
fileContext2 = attentionVs;
FileUtils.write(fileattentionV, fileContext2);
String meditationVs = "" + meditationV;
File filemeditationV = new File("meditationV.php");
fileContext2 = FileUtils.readFileToString(filemeditationV);
fileContext2 = meditationVs;
FileUtils.write(filemeditationV, fileContext2);
Thanks for your help!

Going on just the first sentence where you say you want to parse a JSON file in Java, have you seen this post How to parse JSON in Java

Related

Interpolate JSON values into a string

I am writing an application/class that will take in a template text file and a JSON value and return interpolated text back to the caller.
The format of the input template text file needs to be determined. For example: my name is ${fullName}
Example of the JSON:
{"fullName": "Elon Musk"}
Expected output:
"my name is Elon Musk"
I am looking for a widely used library/formats that can accomplish this.
What format should the template text file be?
What library would support the template text file format defined above and accept JSON values?
Its easy to build my own parser but there are many edge cases that needs to be taken care of and I do not want to reinvent the wheel.
For example, if we have a slightly complex JSON object with lists, nested values etc. then I will have to think about those as well and implement it.
I have always used org.json library. Found at http://www.json.org/.
It makes it really easy to go through JSON Objects.
For example if you want to make a new object:
JSONObject person = new JSONObject();
person.put("fullName", "Elon Musk");
person.put("phoneNumber", 3811111111);
The JSON Object would look like:
{
"fullName": "Elon Musk",
"phoneNumber": 3811111111
}
It's similar to retrieving from the Object
String name = person.getString("fullName");
You can read out the file with BufferedReader and parse it as you wish.
Hopefully I helped out. :)
This is how we do it.
Map inputMap = ["fullName": "Elon Musk"]
String finalText = StrSubstitutor.replace("my name is \${fullName}", inputMap)
You can try this:
https://github.com/alibaba/fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

How to convert JSON to a string with escape characters

I am new to JMeter java and Minidev. I am working on this existing module where I have to use minidev libraries I need to convert a JSON form non-indented format to a string format in Jmeter. The API requires a String representation of the JSON due to some characters.
To give an example, I have {"Item":"Value","MyList":["A","B","C"]} which I want to convert as "{\"Item\":\"Value\",\"MyList\":[\"A\",\"B\",\"C\"]}"
I have tried using JSONObject.toString() or JSONObject.toJSONString() however this does not give me desired results. I am using Jmeter 5 with Java 12
I did not find any good solution in short time so right now I am using below code however I would love to do it the right way.
String postJSON = root.toJSONString().replace("\"","\\\"");
postJSON = postJSON.replace("{","\"{");
postJSON = postJSON.replace("}","}\"");

Reading specific parts of a text file using Scanner class in Java

I have gone through most of the questions related to reading a Text file using the Scanner class here, but I still don't understand how I parse the following text into different variables and then replace that text with the changed variable values.
Here is the text :
## UserID1.Credentials :
[Username] = {default}
[Password] = {passwordforadmin}
[AccType] = {Admin.FirstDefault}
[AccessLevel] = {1}
##
Now, I need the above text parsed into the following variables, where :
int UserID = 1;
String Username = "default";
String Password = "passwordforadmin";
String AccType = "Admin";
String AccUnderType = "FirstDefault";
int AccessLevel = 1;
The method I am using to read the text file is :
String content = new Scanner(new File("D:/EncryptedCredentials.txt")).useDelimiter("\\Z").next();
Where content is the String which reads the text file (D:/EncryptedCredentials.txt) and the delimiter '\Z' is used. (I need to load the whole text file into a String, because it would be continuously modified by the program, and further details should be added automatically, like if the User logged in at a specific time, say 11:00 AM, and logged out at 3:00 PM, and used A,B and C features, then the above text should be modified to :
## UserID1.Credentials :
[Username] = {default}
[Password] = {passwordforadmin}
[AccType] = {Admin.FirstDefault}
[AccessLevel] = {1}
[LastLogInTime] = {1100}
[LastLogOutTime] = {1500}
[TotalTimeSpent] = {0400}
[FeaturesUsed] = {A,B,C}
##
How do I go about this reading and writing to a text file ?
Another question is, since anyone can evidently see I am doing a login system, I need to know how encryption can be implemented into the text file so that people can access the contents of the text file only through the program. The text can be changed, of course. All I need is for the program to read it and store specific information in the appropriate variables, and then to write it back to the encrypted text file.
So, in all, what I need to know is :
How to read only specific parts of a text file loaded onto a String ?
How to modify those parts (including adding text that wasn't present before) and write them again to the text file ?
How to do the above steps while implementing encryption into the text file ?
Please explain them to me, because I don't have much knowledge about this. Thanks in advance.
Note : I'm still a beginner in Java, and I have no idea of anything more than Scanner classes, Arrays, loops, etc. and some random stuff. Please help me out.
I suggest simpler approach by using properties file.
Here is a link to an example:
http://www.mkyong.com/java/java-properties-file-examples/

How would I split this string?

How would I go about cutting this giant string? I need the data of the first ID," preferably for the string output to be:
"id":1,"name":"site1","has_doneit":true,"destination":"4613"
Is this possible? Or if not any other ways of grabbing the name:"site1" and the has_doneit:true would be perfectly fine.
{"needs_complete":true,"has_done":true,"sites":[
{"id":1,"name":"site1","has_doneit":true,"destination":"4613"},{"id":2,"name":"site2","has_doneit":true,"destination":"4613"},{"id":3,"name":"site3","has_doneit":true,"destination":"4339"},{"id":4,"name":"site4","has_doneit":true,"destination":"4340"},{"id":5,"name":"site5","has_doneit":true,"destination":"4341"},
{"id":6,"name":"site6","has_doneit":true,"destination":"4622"},{"id":7,"name":"site7","has_doneit":true,"destination":"4623"},{"id":8,"name":"site8","has_doneit":true,"destination":"4828"},
{"id":9,"name":"site9","has_doneit":true,"destination":"4829"},{"id":10,"name":"site10","has_doneit":true,"destination":"4861"}]}
That seems like a JSON string so use a Json Parser...
PHP: JSON_decode
$parsedStr = json_decode($yourString, true);
you can access sites array in $parsedStr['sites']
So, to access the id of the first site:
echo $parsedStr['sites'][0]['id'];
Java
check this answer in SO Decoding JSON String in Java

Simplest way to read from a JSON file in Java?

I'm relatively new to using JSON and all I really need to do read in a few key value pairs from a JSON file on the file system.
What I figured I would do is read in the file as a string and then parse it that way but it seems kind of redundant that way.
Here's what my file will be like:
{
"username" : "myname"
"domain" : "mydomain"
}
So essentially I need some help making an easy and efficient block of code to read in the key/value pairs. I've been trying to use GSON for the most part and haven't had much luck with examples I've found.
Thanks everyone
One other alternative is JSON.org, in which creating a JSON object from a JSON string requires only one line:
JSONObject jsonObject = new JSONObject(someJSONString);
When you need to access its value, use the functions that the JSONObject provides. For example,
String userName = jsonObject.getString("username");
String domainName = jsonObject.getString("mydomain");

Categories

Resources