package br.com.uniondata.projetodetestes;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
*
* @author Nilson
*/
public class Json {
public static void convertJsonForObjectmain(StringBuilderString[] jsonargs) {
Conection conection = new Conection();
//StringBuilder json=new StringBuilder("{'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}]}");
StringBuilder json=conection.getJson();
JSONObject obj=null;
try{
obj=new JSONObject(json.toString());
System.out.println(obj.toString());
JSONArray jArray = obj.getJSONArray("data");
for(int i = 0; i < jArray.length(); i++){
JSONObject o = jArray.getJSONObject(i);
System.out.println(o.toString());
}
}catch(JSONException e){
e.printStackTrace();
}
}
}
Saída:
--- exec-maven-plugin:1.2.1:exec (default-cli) @ ProjetoDeTestes ---
{"data":[{"validShares":86,"staleShares":5,"lastSeen":1523551133,"currentHashrate":9.916666666666667E7,"averageHashrate":7.309104938271604E7,"invalidShares":0,"time":1523551200,"worker":"kappauni1","reportedHashrate":113159753},{"validShares":103,"staleShares":7,"lastSeen":1523551162,"currentHashrate":119500000,"averageHashrate":6.799305555555557E7,"invalidShares":0,"time":1523551200,"worker":"kappauni2","reportedHashrate":108694107},{"validShares":179,"staleShares":12,"lastSeen":1523551195,"currentHashrate":2.0755555555555555E8,"averageHashrate":1.3074074074074067E8,"invalidShares":0,"time":1523551200,"worker":"kappauni3","reportedHashrate":213535092}],"status":"OK"}
{"validShares":86,"staleShares":5,"lastSeen":1523551133,"currentHashrate":9.916666666666667E7,"averageHashrate":7.309104938271604E7,"invalidShares":0,"time":1523551200,"worker":"kappauni1","reportedHashrate":113159753}
{"validShares":103,"staleShares":7,"lastSeen":1523551162,"currentHashrate":119500000,"averageHashrate":6.799305555555557E7,"invalidShares":0,"time":1523551200,"worker":"kappauni2","reportedHashrate":108694107}
{"validShares":179,"staleShares":12,"lastSeen":1523551195,"currentHashrate":2.0755555555555555E8,"averageHashrate":1.3074074074074067E8,"invalidShares":0,"time":1523551200,"worker":"kappauni3","reportedHashrate":213535092}
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.569s
Finished at: Thu Apr 12 13:50:44 BRT 2018
Final Memory: 5M/123M
------------------------------------------------------------------------