Skip to content

Instantly share code, notes, and snippets.

@anta40
Created March 15, 2020 15:40
Show Gist options
  • Save anta40/9de3ab11e375937db52d06f6fe333828 to your computer and use it in GitHub Desktop.
Save anta40/9de3ab11e375937db52d06f6fe333828 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
class TogelTest {
public static void main(String args[]){
try {
// baca JSON dari text file
String json = new String(Files.readAllBytes(Paths.get(args[0])));
System.out.println(json);
JSONArray jsonArray = new JSONArray(json);
List<Togel> listTogel = new ArrayList<Togel>();
Set<Integer> set = new HashSet<Integer>();
// proses isi JSON, simpan di list
int len = jsonArray.length();
for (int x = 0; x < len; x++){
JSONObject obj = jsonArray.getJSONObject(x);
Togel togel = new Togel(obj.getInt("nomortogel"), obj.getInt("banyak"));
if (!set.contains(togel.getBanyak())){
set.add(togel.getBanyak());
listTogel.add(togel);
}
}
for (int x = 0; x < listTogel.size(); x++){
System.out.println(listTogel.get(x));
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException je){
je.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment