JSONArray

Hi guys, I am getting the following error when I am trying to run the code in the pastebin link and I dont really understand why as I copied from a doc which worked on the video....
Exception in thread "main" java.lang.ClassCastException: class org.json.simple.JSONObject cannot be cast to class org.json.simple.JSONArray (org.json.simple.JSONObject and org.json.simple.JSONArray are in unnamed module of loader 'app')
at main.ConfigurationHandler.readConfigFile(ConfigurationHandler.java:32)
at main.MainS.main(MainS.java:21)
Exception in thread "main" java.lang.ClassCastException: class org.json.simple.JSONObject cannot be cast to class org.json.simple.JSONArray (org.json.simple.JSONObject and org.json.simple.JSONArray are in unnamed module of loader 'app')
at main.ConfigurationHandler.readConfigFile(ConfigurationHandler.java:32)
at main.MainS.main(MainS.java:21)
https://pastebin.com/hGiZ1gz0 Halwp
Pastebin
dc - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
44 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @netty.bootstrap! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
netty
nettyOP7mo ago
Also, if anyone has a better idea to read out of a json file, let me know!
tjoener
tjoener7mo ago
ah well, it says you're trying to get an object when it's an array soooooooo what does the source look like?
netty
nettyOP7mo ago
Uh what source do you mean I sent a pastebin
szatkus
szatkus7mo ago
Apparently you have an object in your json file, but you have code that expects an array.
dan1st
dan1st7mo ago
Yeah this is a thing of json-simple If you have a {}, you need to cast to JSONObject, not JSONArray at that position Also it may be better to use json-java instead of json-simple json-simple is a bit questionable or you could use an object mapper
netty
nettyOP7mo ago
ok can you send an example
szatkus
szatkus7mo ago
Maybe read about JSON before tackling this project?
dan1st
dan1st7mo ago
of what exactly?
netty
nettyOP7mo ago
usage of json java or json java in general
dan1st
dan1st7mo ago
Can you show your JSON? btw in your code, the line JSONArray dbList = (JSONArray) obj; is likely the issue
szatkus
szatkus7mo ago
Wait, you learn from a video of someone who writes code like this?
private void parseObj(JSONObject emp) {
JSONObject empObj = (JSONObject) emp.get("database");
//Get emp firstname
SERVER_IP = (String) empObj.get("SERVER_IP");
private void parseObj(JSONObject emp) {
JSONObject empObj = (JSONObject) emp.get("database");
//Get emp firstname
SERVER_IP = (String) empObj.get("SERVER_IP");
dan1st
dan1st7mo ago
that's how json-simple works and why I wouldn't use it
tjoener
tjoener7mo ago
or Gson, I kinda like their object model
szatkus
szatkus7mo ago
Nah, it has getString. And nothing explains that double cast.
netty
nettyOP7mo ago
netty
nettyOP7mo ago
yup
Kyo-chan
Kyo-chan7mo ago
So that's not an array. What do you mean, "it worked on the video"?
netty
nettyOP7mo ago
erm idk
szatkus
szatkus7mo ago
That looks... very crude.
Kyo-chan
Kyo-chan7mo ago
You... Don't know what you meant? It's like when you add random words in a sentence, Que sera sera, you don't know what it means it's just for style?
netty
nettyOP7mo ago
Dude its obvious what I meant by the question I asked
Kyo-chan
Kyo-chan7mo ago
What it obviously means is that you watched a video of a guy who lied and was saying that given some files, trying to run the entire project doesn't fail in the same way yours fail So unless you want to tell me that what it meant isn't the obvious...
netty
nettyOP7mo ago
What? My question was, how I can read values out of a json file I have never used that package before so how would I know its not working when you are using an array for an object
Kyo-chan
Kyo-chan7mo ago
And my question was, what do you mean it worked on the video?
netty
nettyOP7mo ago
well he had the code in his main method and the config and it worked
szatkus
szatkus7mo ago
There's nothing that looks like an array in that JSON file.
netty
nettyOP7mo ago
I can tell that too
Kyo-chan
Kyo-chan7mo ago
And you ensured you have the same code everywhere. So, fundamentally, you watched a video of a guy lying? Frustratingly enough I have not yet found a JSON library I like. I make mine for whenever I use JSON for personal purpose.
szatkus
szatkus7mo ago
I don't know, Gson is pretty good. Although I usually use Jackson for a sole reason that it's usually already in dependencies.
tjoener
tjoener7mo ago
yeah, I just wish gson was split That all the reflection based stuff was a separate library
netty
nettyOP7mo ago
I have checked and tried a few now but still could not find any good libary to do what I wanna do Might just stick with vertx
tjoener
tjoener7mo ago
vertx is slaying a mosquito with an elephant
dan1st
dan1st7mo ago
if you want one that works similar to json-simple (i.e you tell it what to access), you can use json-java:
JSONObject root = new JSONObject(yourJsonString);
String someElement = root.getString("abc");
JSONObject nestedObject = root.getJSONObject("def");
JSONObject root = new JSONObject(yourJsonString);
String someElement = root.getString("abc");
JSONObject nestedObject = root.getJSONObject("def");
if you want something that converts between JSON and your objects automatically, use gson or jackson
netty
nettyOP7mo ago
ok ill try
netty
nettyOP7mo ago
Ok so I managed to (kinda) do what I wanna do now with vertx, but now is my question... how do I access specific values or strings now, which are returned? Code I used:
package src;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import io.vertx.core.json.JsonObject;

public class Main {

public static void main(String[] args) {
try {

String jsonStr = IOUtils.toString(new FileReader("config.json"));
JsonObject jsonObj = new JsonObject(jsonStr);
System.out.print(jsonObj.getString("database"));

} catch(FileNotFoundException fnfe) {
System.out.print("File not found");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
package src;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import io.vertx.core.json.JsonObject;

public class Main {

public static void main(String[] args) {
try {

String jsonStr = IOUtils.toString(new FileReader("config.json"));
JsonObject jsonObj = new JsonObject(jsonStr);
System.out.print(jsonObj.getString("database"));

} catch(FileNotFoundException fnfe) {
System.out.print("File not found");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
No description
szatkus
szatkus7mo ago
Lol, vert.x has its own JSON parser?
netty
nettyOP7mo ago
yu It works fire and vertx is a very good libary at all
szatkus
szatkus7mo ago
Anyway, something like jsonObj.getJsonObject("database").getString("SERVER_IP") should work.
netty
nettyOP7mo ago
ahhh omg i am dumb obv ill try tomorrow doe, thanks mate
dan1st
dan1st7mo ago
looks like a json-java wrapper or repackaging
tjoener
tjoener7mo ago
Using vertx is completely wrong for this, unless you're already using as a webserver
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server