Estou com uma pequena dificuldade em obter informações de um Array de Json, pois esta dando o erro:
Exception in thread "main" json.org.JSONException: JSONArray initial value should be a string or collection or array.
A saida do json é:
{"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}]}
Seguem as classes, agradeceria muito se dessem uma luz.
CLASSE MAIN
public class Main {
public static void main(String[]args){
Conection conection = new Conection();
Convert convert = new Convert();
convert.convertJsonForObject(conection.getJson());
}
}
Classe responsável por fazer conexão e gerar um return em StringBuilder.
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;
}
}
Classe responsável por converter Json em objeto, aqui que não consigo obter corretamente os dados do Array de Json.
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());
}
}
}
A saida do erro é :
{"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
jArray = new JSONArray(json.toString());