Example usage for org.json.simple JSONArray iterator

List of usage examples for org.json.simple JSONArray iterator

Introduction

In this page you can find the example usage for org.json.simple JSONArray iterator.

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this list in proper sequence.

Usage

From source file:com.atlauncher.data.Settings.java

public void checkMojangStatus() {
    JSONParser parser = new JSONParser();
    try {/*from ww  w. j av a 2 s .  c om*/
        Downloadable download = new Downloadable("http://status.mojang.com/check", false);
        String response = download.getContents();
        if (response == null) {
            minecraftSessionServerUp = false;
            minecraftLoginServerUp = false;
            return;
        }
        Object obj = parser.parse(response);
        JSONArray jsonObject = (JSONArray) obj;
        Iterator<JSONObject> iterator = jsonObject.iterator();
        while (iterator.hasNext()) {
            JSONObject object = iterator.next();
            if (object.containsKey("authserver.mojang.com")) {
                if (((String) object.get("authserver.mojang.com")).equalsIgnoreCase("green")) {
                    minecraftLoginServerUp = true;
                }
            } else if (object.containsKey("session.minecraft.net")) {
                if (((String) object.get("session.minecraft.net")).equalsIgnoreCase("green")) {
                    minecraftSessionServerUp = true;
                }
            }
        }
    } catch (ParseException e) {
        this.logStackTrace(e);
        minecraftSessionServerUp = false;
        minecraftLoginServerUp = false;
    }
}

From source file:nl.utwente.bigdata.bolts.WorldCupJsonToDataBolt.java

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    try {/*ww w  .java 2s .co  m*/
        JSONObject game = (JSONObject) parser.parse(tuple.getString(0));

        JSONArray officials = (JSONArray) game.get("officials");
        JSONObject home = (JSONObject) game.get("home");
        JSONObject away = (JSONObject) game.get("away");

        String referee_name = "";
        String home_name = (String) home.get("name");
        String away_name = (String) away.get("name");

        SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy - k:mm");
        Date matchTime = new Date();
        matchTime = formatter.parse((String) game.get("time"));
        Iterator i = officials.iterator();

        while (i.hasNext()) {
            JSONObject official = (JSONObject) i.next();
            if (official.get("role").equals("Referee")) {
                referee_name = (String) official.get("name");
            }
        }
        //System.out.println(referee_name);
        collector.emit(new Values(referee_name, home_name, away_name, matchTime));
    } catch (ClassCastException e) {
        System.out.println("ClassCass");
        e.printStackTrace();
        return; // do nothing (we might log this)
    } catch (org.json.simple.parser.ParseException e) {
        System.out.println("ParseException");
        e.printStackTrace();
        return; // do nothing 
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:nl.utwente.bigdata.spouts.WorldcupDataJsonSpout.java

@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    try {/*  w  w  w  . j  a va2 s.c  om*/
        System.out.println("Reading worldcup-data");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(getClass().getClassLoader().getResourceAsStream("worldcup-games.json")));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null)
            sb.append(line);
        JSONParser parser = new JSONParser();
        JSONArray collection = (JSONArray) parser.parse(sb.toString());
        Iterator iter = collection.iterator();
        while (iter.hasNext()) {
            JSONObject entry = (JSONObject) iter.next();
            matches.add(JSONValue.toJSONString(entry));
            //    System.out.println(JSONValue.toJSONString( entry ));
        }
        reader.close();
        System.out.println("Reading done");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    _collector = collector;
    _rand = new Random();
}

From source file:no.oddsor.simulator3.json.TaskReader.java

public Collection<ITask> getTasks() {
    Collection<ITask> tasks = new ArrayList<>();
    if (object == null)
        return tasks;
    JSONArray tasklist = (JSONArray) object.get("Activities");
    Iterator taskIterator = tasklist.iterator();
    while (taskIterator.hasNext()) {
        JSONObject nextTask = (JSONObject) taskIterator.next();
        Task newTask;/*from   w ww. j a  va2s. c  o  m*/
        if (nextTask.containsKey("Timespan")) {
            JSONArray timespan = (JSONArray) nextTask.get("Timespan");
            tasks.add(newTask = new Task((String) nextTask.get("Name"), (String) nextTask.get("Type"),
                    Integer.parseInt(nextTask.get("Duration").toString()),
                    Integer.parseInt(timespan.get(0).toString()), Integer.parseInt(timespan.get(1).toString()),
                    (String) nextTask.get("Appliance"),
                    (nextTask.containsKey("Label") ? (String) nextTask.get("Label") : null)));
        } else {
            tasks.add(newTask = new Task((String) nextTask.get("Name"), (String) nextTask.get("Type"),
                    Integer.parseInt(nextTask.get("Duration").toString()), (String) nextTask.get("Appliance"),
                    (nextTask.containsKey("Label") ? (String) nextTask.get("Label") : null)));
        }
        if (nextTask.containsKey("Cooldown")) {
            double d = (long) nextTask.get("Cooldown") * 3600.0;
            newTask.setCooldown(d);
        }
        if (nextTask.containsKey("IncreasesNeed")) {
            JSONArray need = (JSONArray) nextTask.get("IncreasesNeed");
            newTask.addResult((String) need.get(0), Integer.parseInt(need.get(1).toString()));
        }
        if (nextTask.containsKey("RequiresItem")) {
            if (!(nextTask.get("RequiresItem") instanceof JSONArray)) {
                newTask.addRequiredItem((String) nextTask.get("RequiresItem"), 1);
            } else {
                JSONArray requirements = (JSONArray) nextTask.get("RequiresItem");
                if (requirements.get(1) instanceof Number) {
                    newTask.addRequiredItem((String) requirements.get(0),
                            Integer.parseInt(requirements.get(1).toString()));
                } else {
                    for (Object item : requirements) {
                        newTask.addRequiredItem((String) item, 1);
                    }
                }
            }
        }
        if (nextTask.containsKey("Creates")) {
            if (nextTask.get("Creates") instanceof JSONArray) {
                JSONArray creates = (JSONArray) nextTask.get("Creates");
                newTask.addResultingItem((String) creates.get(0), Integer.parseInt(creates.get(1).toString()));
            } else
                newTask.addResultingItem((String) nextTask.get("Creates"), 1);
        }
        if (nextTask.containsKey("PoseFeatures")) {
            if (nextTask.get("PoseFeatures") instanceof JSONArray) {
                JSONArray poses = (JSONArray) nextTask.get("PoseFeatures");
                for (Object pose : poses) {
                    String poseString = (String) pose;
                    newTask.addPose(poseString);
                }
            }
        }
        if (nextTask.containsKey("NeedsState")) {
            if (nextTask.get("NeedsState") instanceof JSONArray) {
                JSONArray poses = (JSONArray) nextTask.get("NeedsState");
                for (Object pose : poses) {
                    String poseString = (String) pose;
                    newTask.addNeededState(poseString);
                }
            }
        }
        if (nextTask.containsKey("AddsState")) {
            if (nextTask.get("AddsState") instanceof JSONArray) {
                JSONArray poses = (JSONArray) nextTask.get("AddsState");
                for (Object pose : poses) {
                    String poseString = (String) pose;
                    if (poseString.charAt(0) == '+')
                        newTask.addPlusState(poseString.substring(1));
                    if (poseString.charAt(0) == '-')
                        newTask.addMinusState(poseString.substring(1));
                }
            }
        }
    }
    return tasks;
}

From source file:no.oddsor.simulator3.json.TaskReader.java

public Set<String> getAppliances() {
    Set<String> appliances = new HashSet<>();
    if (object == null)
        return appliances;
    JSONArray tasklist = (JSONArray) object.get("Activities");
    Iterator tasks = tasklist.iterator();
    while (tasks.hasNext()) {
        JSONObject task = (JSONObject) tasks.next();
        if (task.containsKey("Appliance"))
            appliances.add((String) task.get("Appliance"));
        if (task.containsKey("Creates") && task.get("Creates") instanceof JSONArray) {
            JSONArray createArray = (JSONArray) task.get("Creates");
            if (createArray.get(1) instanceof String)
                appliances.add(createArray.get(1).toString());
        }//from   ww w  .java  2  s  .  c om
    }
    return appliances;
}

From source file:no.oddsor.simulator3.json.TaskReader.java

public List<Need> getNeeds() {
    List<Need> needs = new ArrayList<>();
    if (object == null)
        return needs;
    JSONArray taskList = (JSONArray) object.get("Activities");
    Set<String> needNames = new HashSet<>();
    Iterator tasks = taskList.iterator();
    while (tasks.hasNext()) {
        JSONObject task = (JSONObject) tasks.next();
        if (task.containsKey("IncreasesNeed")) {
            JSONArray arr = (JSONArray) task.get("IncreasesNeed");
            needNames.add((String) arr.get(0));
        }/*www.  j a va  2 s . co  m*/
    }
    for (String name : needNames) {
        needs.add(new Need(name, 1.0));
    }
    return needs;
}

From source file:ordenesCompra.CargarDatos.java

/**
 * @param args the command line arguments
 */// w ww  . j a va 2s.  com
public static void main(String[] args) {

    JSONParser parser = new JSONParser();

    try {
        FileReader fileReader = new FileReader("p.json");
        Object obj = parser.parse(fileReader);
        //            JSONArray msg = (JSONArray) jsonObject.get("categories");

        JSONArray msg = (JSONArray) obj;
        Iterator<JSONObject> iterator = msg.iterator();
        System.out.println(msg.size());
        while (iterator.hasNext()) {
            JSONObject json = iterator.next();

            String name = (String) json.get("asin");
            System.out.println(name);

            name = (String) json.get("group");
            System.out.println(name);

            name = (String) json.get("title");
            System.out.println(name);

            JSONObject review = (JSONObject) json.get("review_info");
            JSONArray reviews = (JSONArray) review.get("reviews");

            Iterator<JSONObject> iteratorReview = reviews.iterator();

            while (iteratorReview.hasNext()) {
                JSONObject reviewObject = iteratorReview.next();
                System.out.println(reviewObject.get("customer"));
                System.out.println(reviewObject.get("rating"));
                System.out.println(reviewObject.get("votes"));
                System.out.println(reviewObject.get("helpful"));

            }
            Long long1 = (Long) review.get("total");
            System.out.println(long1);
            Double double1 = (Double) review.get("avg");
            System.out.println(double1);

        }

        //            String name = (String) jsonObject.get("asin");
        //            System.out.println(name);
        //          String name2 =  (String) jsonObject.get("categories");
        //            System.out.println(name2);
        // loop array
        //            JSONArray msg = (JSONArray) jsonObject.get("categories");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:ordenesCompra.CargarDatos1.java

/**
 * @param args the command line arguments
 */// w ww. j  a v a  2  s .c  om
public static void main(String[] args) {

    JSONParser parser = new JSONParser();

    try {
        FileReader fileReader = new FileReader("/opt/productos2.json");
        Object obj = parser.parse(fileReader);
        //            JSONArray msg = (JSONArray) jsonObject.get("categories");

        JSONArray msg = (JSONArray) obj;
        Iterator<JSONObject> iterator = msg.iterator();
        System.out.println(msg.size());
        while (iterator.hasNext()) {
            JSONObject json = iterator.next();
            System.out.println(json.toJSONString());
            String jsonstr = json.toJSONString();
            CouchDbClient dbClient2 = new CouchDbClient("productos", true, "http", "192.168.0.3", 5984, null,
                    null);
            JsonObject jsonobj = dbClient2.getGson().fromJson(jsonstr, JsonObject.class);
            dbClient2.save(jsonobj);

        }

        //            String name = (String) jsonObject.get("asin");
        //            System.out.println(name);
        //          String name2 =  (String) jsonObject.get("categories");
        //            System.out.println(name2);
        // loop array
        //            JSONArray msg = (JSONArray) jsonObject.get("categories");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

From source file:org.alfresco.dataprep.ContentActions.java

/**
 * Return a list of tags or comments/*from   w ww .  ja  v a  2s.c  om*/
 * 
 * @param response HttpResponse as String
 * @param optType content actions (e.g. tag, comments, likes)
 */
@SuppressWarnings("unchecked")
private List<String> getOptionValues(final HttpResponse response, final ActionType optType) {
    AlfrescoHttpClient client = alfrescoHttpClientFactory.getObject();
    JSONArray jArray = client.getJSONArray(response, "list", "entries");
    List<String> values = new ArrayList<String>();
    Iterator<JSONObject> iterator = jArray.iterator();
    while (iterator.hasNext()) {
        JSONObject factObj = (JSONObject) iterator.next();
        JSONObject entry = (JSONObject) factObj.get("entry");
        values.add((String) entry.get(optType.bodyParam));
    }
    return values;
}

From source file:org.alfresco.dataprep.ContentActions.java

/**
 * Return the node ref for a tag or comment
 * //from  w ww  . ja va2  s. com
 * @param response HttpResponse as String
 * @param value value of tag or comment
 * @param optType content actions (e.g. tag, comments, likes)
 */
@SuppressWarnings("unchecked")
private String getOptionNodeRef(final HttpResponse response, final String value, final ActionType optType) {
    String nodeRef = "";
    AlfrescoHttpClient jClient = alfrescoHttpClientFactory.getObject();
    JSONArray jArray = jClient.getJSONArray(response, "list", "entries");
    Iterator<JSONObject> iterator = jArray.iterator();
    while (iterator.hasNext()) {
        JSONObject factObj = (JSONObject) iterator.next();
        JSONObject entry = (JSONObject) factObj.get("entry");
        String name = (String) entry.get(optType.bodyParam);
        if (name.equalsIgnoreCase(value)) {
            nodeRef = (String) entry.get("id");
        }
    }
    return nodeRef;
}