2
I’m having a little difficulty getting information from an Array of Json, as it is giving the error:
Exception in thread "main" json.org.Jsonexception: Jsonarray initial value should be a string or Collection or array.
The json output is:
{"status":"OK","data":[{"worker":"kappauni1","time":1523537400,"lastSeen":1523537364,"reportedHashrate":113223366,"currentHashrate":102500000,"validShares":89,"invalidShares":0,"staleShares":5,"averageHashrate":68804012.34567899},{"worker":"kappauni2","time":1523537400,"lastSeen":1523537351,"reportedHashrate":108686214,"currentHashrate":110277777.77777778,"validShares":96,"invalidShares":0,"staleShares":5,"averageHashrate":62395061.72839507},{"worker":"kappauni3","time":1523537400,"lastSeen":1523537396,"reportedHashrate":211388916,"currentHashrate":175388888.8888889,"validShares":152,"invalidShares":0,"staleShares":9,"averageHashrate":120002314.8148148}]}
Follow the classes, I’d appreciate it if you’d shed some light.
MAIN CLASS
public class Main {
public static void main(String[]args){
Conection conection = new Conection();
Convert convert = new Convert();
convert.convertJsonForObject(conection.getJson());
}
}
Class responsible for connecting and generating a Stringbuilder Return.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
class Conection
{
private StringBuilder resultado = new StringBuilder();
StringBuilder getJson()
{
String address = "https://api.ethermine.org/miner/0x0940f5fAEF2bba7e1e6288E4bc4E9c75ee334b97/workers";
try {
URL url = new URL(address);
try {
URLConnection connect = url.openConnection();
connect.setRequestProperty("User-Agent", "Mozilla/4.0");
InputStream in = connect.getInputStream();
InputStreamReader inRead = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inRead);
String result;
while((result = reader.readLine())!=null){
this.resultado = resultado.append(result);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return resultado;
}
}
Class responsible for converting Json into object, here I cannot get the data from the Json Array correctly.
import json.org.JSONArray;
import json.org.JSONObject;
class Convert {
void convertJsonForObject(StringBuilder json){
System.out.println(json);
JSONObject obj = new JSONObject(json);
JSONArray jArray = obj.getJSONArray("data");
for(int i = 0; i < jArray.length(); i++){
obj = jArray.getJSONObject(i);
System.out.print(obj.toString());
}
}
}
The exit from error is :
{"status":"OK","data":[{"worker":"kappauni1","time":1523548800,"lastSeen":1523548743,"reportedHashrate":113155830,"currentHashrate":95833333.33333333,"validShares":83,"invalidShares":0,"staleShares":5,"averageHashrate":69709876.54320988},{"worker":"kappauni2","time":1523548800,"lastSeen":1523548779,"reportedHashrate":108667905,"currentHashrate":86944444.44444445,"validShares":75,"invalidShares":0,"staleShares":5,"averageHashrate":64443672.83950619},{"worker":"kappauni3","time":1523548800,"lastSeen":1523548783,"reportedHashrate":213519635,"currentHashrate":167611111.1111111,"validShares":145,"invalidShares":0,"staleShares":9,"averageHashrate":123407407.40740736}]}
Exception in thread "main" json.org.JSONException: JSONObject["data"] not found.
at json.org.JSONObject.get(JSONObject.java:566)
at json.org.JSONObject.getJSONArray(JSONObject.java:760)
at Convert.convertJsonForObject(Convert.java:11)
at Main.main(Main.java:10)
Process finished with exit code 1
On which line is fired the exception?
– Giuliana Bezerra
The Jsonarray constructor expects a string, and you are passing an instance of Stringbuilder. Try :
jArray = new JSONArray(json.toString());
– Marcus Vinicius
I’ve updated you at this point, if you could just take a peek.
– Diego Kappaun