Reputation: 123
1st Question:
Im creating an IRC Bot in java using Pircbot that implements the OpenWeatherMap API, and I'm having trouble displaying an initial message that is sent from the bot as soon as it connects to the channel. I want this message to display instructions on how to use the APIs. I tried doing this in the constructor, but that didn't work as you need the channel string as seen in the onMessage method. I searched through the methods in the Pircbot website but couldn't find a method for this.
2nd Question:
I'm having problems implementing a certain part of OpenWeatherMap's API. For "weather", it uses a JsonArray and I'm not entirely sure how to parse it. Because it's an API and not a file, the solutions I've found online haven't been working because they use JsonReader while I'm trying to use JsonParser. Here's my code trying to parse this Array. I'm trying to access the "main" key from the "weather" JsonArray.
static String parseJsonWeatherMain(String json)
{
JsonElement jelement = new JsonParser().parse(json);
JsonObject MasterWeatherObject = jelement.getAsJsonObject();
JsonArray weatherArray = MasterWeatherObject.getAsJsonArray("weather");
String main = weatherArray.get(1).getAsString();
return main;
}
For reference, this is how I parsed the other keys that were just from JsonObjects:
static double parseJsonWindGust(String json)
{
JsonElement jelement = new JsonParser().parse(json);
JsonObject MasterWeatherObject = jelement.getAsJsonObject();
JsonObject windObject = MasterWeatherObject.getAsJsonObject("wind");
double gust = windObject.get("gust").getAsDouble();
return gust;
}
so any ideas on how to parse this JsonArray? I want the "main" and "description" keys to be exact.
Upvotes: 0
Views: 203
Reputation: 12196
Well.. I'm not a real Java developer, but i will give it a shot.
Question 1: Publishing message when bot joins a channel
This sounds like a classic onJoin event.
From PircBot documentation
protected void onJoin(String channel,
String sender,
String login,
String hostname)
This method is called whenever someone (possibly us) joins a channel which we are on.
The implementation of this method in the PircBot abstract class performs no actions and may be overridden as required.
Parameters:
channel - The channel which somebody joined.
sender - The nick of the user who joined the channel.
login - The login of the user who joined the channel.
hostname - The hostname of the user who joined the channel.
Question2: Extracting main property from JObject which inside a JArray.
You came close to the solution, you only forgot a very basic thing.
weather is an array of objects, thus you should expect weatherArray.get(1)
will return an object, an object of which you should then apply .get("main")
to extract json object property named "main", which only the you can apply .getAsString()
because it's a normal string.
Code (not been tested, but the idea is understandable)
JsonArray weatherArray = MasterWeatherObject.getAsJsonArray("weather");
for (int i = 0; i < weatherArray.size(); i++) {
String main = weatherArray.get("main").getAsString();
System.out.println(main);
}
Upvotes: 0