Java Code Examples for org.codehaus.jettison.json.JSONArray#optJSONObject()

The following examples show how to use org.codehaus.jettison.json.JSONArray#optJSONObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From tez with Apache License 2.0 6 votes vote down vote up
/**
 * Parse events from json
 *
 * @param eventNodes
 * @param eventList
 * @throws JSONException
 */
public static void parseEvents(JSONArray eventNodes, List<Event> eventList) throws
    JSONException {
  if (eventNodes == null) {
    return;
  }
  for (int i = 0; i < eventNodes.length(); i++) {
    JSONObject eventNode = eventNodes.optJSONObject(i);
    final String eventInfo = eventNode.optString(Constants.EVENT_INFO);
    final String eventType = eventNode.optString(Constants.EVENT_TYPE);
    final long time = eventNode.optLong(Constants.EVENT_TIME_STAMP);

    Event event = new Event(eventInfo, eventType, time);

    eventList.add(event);

  }
}
 
Example 2
Source File: Utils.java    From tez with Apache License 2.0 5 votes vote down vote up
/**
 * Parse tez counters from json
 *
 * @param jsonObject
 * @return TezCounters
 * @throws JSONException
 */
public static TezCounters parseTezCountersFromJSON(JSONObject jsonObject)
    throws JSONException {
  TezCounters counters = new TezCounters();

  if (jsonObject == null) {
    return counters; //empty counters.
  }

  final JSONArray counterGroupNodes = jsonObject.optJSONArray(Constants.COUNTER_GROUPS);
  if (counterGroupNodes != null) {
    for (int i = 0; i < counterGroupNodes.length(); i++) {
      JSONObject counterGroupNode = counterGroupNodes.optJSONObject(i);
      final String groupName = counterGroupNode.optString(Constants.COUNTER_GROUP_NAME);
      final String groupDisplayName = counterGroupNode.optString(
          Constants.COUNTER_GROUP_DISPLAY_NAME, groupName);

      CounterGroup group = counters.addGroup(groupName, groupDisplayName);

      final JSONArray counterNodes = counterGroupNode.optJSONArray(Constants.COUNTERS);

      //Parse counter nodes
      for (int j = 0; j < counterNodes.length(); j++) {
        JSONObject counterNode = counterNodes.optJSONObject(j);
        final String counterName = counterNode.getString(Constants.COUNTER_NAME);
        final String counterDisplayName =
            counterNode.optString(Constants.COUNTER_DISPLAY_NAME, counterName);
        final long counterValue = counterNode.getLong(Constants.COUNTER_VALUE);
        addCounter(group, counterName, counterDisplayName, counterValue);
      }
    }
  }
  return counters;
}