List of usage examples for org.json JSONArray get
public Object get(int index) throws JSONException
From source file:com.oakesville.mythling.util.MythTvParser.java
private TvShow buildLiveTvItem(JSONObject chanInfo) throws JSONException, ParseException { String chanId = chanInfo.getString("ChanId"); if (!chanInfo.has("Programs")) return null; JSONArray progs = chanInfo.getJSONArray("Programs"); if (progs.length() == 0) return null; JSONObject prog = (JSONObject) progs.get(0); String startTime = prog.getString("StartTime").replace('T', ' '); if (startTime.endsWith("Z")) startTime = startTime.substring(0, startTime.length() - 1); String id = chanId + "~" + startTime; TvShow tvShow = new TvShow(id, prog.getString("Title")); tvShow.setStartTime(parseMythDateTime(startTime)); tvShow.setProgramStart(startTime);/*ww w. j a v a2 s.c o m*/ if (chanInfo.has("ChanNum")) tvShow.setChannelNumber(chanInfo.getString("ChanNum")); if (chanInfo.has("CallSign")) tvShow.setCallsign(chanInfo.getString("CallSign")); addProgramInfo(tvShow, prog); if (prog.has("transcoded")) tvShow.setTranscoded("true".equalsIgnoreCase(prog.getString("transcoded"))); return tvShow; }
From source file:com.oakesville.mythling.util.MythTvParser.java
public List<LiveStreamInfo> parseStreamInfoList() { List<LiveStreamInfo> streamList = new ArrayList<LiveStreamInfo>(); try {/*from w w w . j a v a 2s . c o m*/ long startTime = System.currentTimeMillis(); JSONObject list = new JSONObject(json).getJSONObject("LiveStreamInfoList"); if (list.has("LiveStreamInfos")) { JSONArray infos = list.getJSONArray("LiveStreamInfos"); for (int i = 0; i < infos.length(); i++) { JSONObject info = (JSONObject) infos.get(i); streamList.add(buildLiveStream(info)); } } if (BuildConfig.DEBUG) Log.d(TAG, " -> live stream info parse time: " + (System.currentTimeMillis() - startTime) + " ms"); } catch (Exception ex) { Log.e(TAG, ex.getMessage(), ex); if (appSettings.isErrorReportingEnabled()) new Reporter(ex).send(); } return streamList; }
From source file:com.oakesville.mythling.util.MythTvParser.java
public Map<String, StorageGroup> parseStorageGroups() throws JSONException { Map<String, StorageGroup> storageGroups = new HashMap<String, StorageGroup>(); JSONObject dirList = new JSONObject(json).getJSONObject("StorageGroupDirList"); if (dirList.has("StorageGroupDirs")) { JSONArray dirs = dirList.getJSONArray("StorageGroupDirs"); for (int i = 0; i < dirs.length(); i++) { JSONObject dir = (JSONObject) dirs.get(i); String name = dir.getString("GroupName"); StorageGroup storageGroup = storageGroups.get(name); if (storageGroup == null) { storageGroup = new StorageGroup(name); storageGroups.put(name, storageGroup); }// w ww .ja v a 2 s. co m String dirPath = dir.getString("DirName"); if (dirPath.endsWith("/")) dirPath = dirPath.substring(0, dirPath.length() - 1); storageGroup.addDirectory(dirPath); if (dir.has("HostName")) storageGroup.setHost(dir.getString("HostName")); } } return storageGroups; }
From source file:com.oakesville.mythling.util.MythTvParser.java
public Map<String, ChannelGroup> parseChannelGroups() throws JSONException { Map<String, ChannelGroup> channelGroups = new HashMap<String, ChannelGroup>(); JSONObject jsonObj = new JSONObject(json); if (jsonObj.has("ChannelGroupList")) { // will not for 0.27 JSONObject groupList = jsonObj.getJSONObject("ChannelGroupList"); if (groupList.has("ChannelGroups")) { JSONArray groups = groupList.getJSONArray("ChannelGroups"); for (int i = 0; i < groups.length(); i++) { JSONObject group = (JSONObject) groups.get(i); String name = group.getString("Name"); String id = group.getString("GroupId"); ChannelGroup channelGroup = new ChannelGroup(id, name); channelGroups.put(name, channelGroup); }//from ww w . ja v a2s .c om } } return channelGroups; }
From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java
public RequestPacket(JSONObject json) throws JSONException { super(json);/*from w w w .ja v a 2s . c om*/ this.packetType = PaxosPacketType.REQUEST; this.stop = json.optBoolean(Keys.STOP.toString()); this.requestID = json.getLong(Keys.QID.toString()); this.requestValue = json.getString(Keys.QV.toString()); this.responseValue = json.has(Keys.RV.toString()) ? json.getString(Keys.RV.toString()) : null; this.entryTime = json.getLong(Keys.ET.toString()); this.forwardCount = (json.has(Keys.NFWDS.toString()) ? json.getInt(Keys.NFWDS.toString()) : 0); this.forwarderID = (json.has(RequestPacket.Keys.FWDR.toString()) ? json.getInt(RequestPacket.Keys.FWDR.toString()) : IntegerMap.NULL_INT_NODE); this.debugInfo = (json.has(Keys.DBG.toString()) ? json.getString(Keys.DBG.toString()) : ""); this.clientAddress = (json.has(Keys.CA.toString()) ? Util.getInetSocketAddressFromString(json.getString(Keys.CA.toString())) : JSONNIOTransport.getSenderAddress(json)); this.listenAddress = (json.has(Keys.LA.toString()) ? Util.getInetSocketAddressFromString(json.getString(Keys.LA.toString())) : JSONNIOTransport.getReceiverAddress(json)); this.entryReplica = json.getInt(PaxosPacket.NodeIDKeys.E.toString()); this.shouldReturnRequestValue = json.optBoolean(Keys.QF.toString()); // unwrap latched along batch JSONArray batchedJSON = json.has(Keys.BATCH.toString()) ? json.getJSONArray(Keys.BATCH.toString()) : null; if (batchedJSON != null && batchedJSON.length() > 0) { this.batched = new RequestPacket[batchedJSON.length()]; for (int i = 0; i < batchedJSON.length(); i++) { this.batched[i] = new RequestPacket((JSONObject) batchedJSON.get(i) // new JSONObject(batchedJSON.getString(i)) ); } } // we remembered the original string for recalling here this.stringifiedSelf = json.has(Keys.STRINGIFIED.toString()) ? (String) json.get(Keys.STRINGIFIED.toString()) : null; if (json.has(Keys.BC.toString())) this.broadcasted = json.getBoolean(Keys.BC.toString()); if (json.has(Keys.DIG.toString())) try { this.digest = json.getString(Keys.DIG.toString()).getBytes(CHARSET); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
From source file:com.sgu.findyourfriend.utils.MessageParser.java
public static ArrayList<Message> parse(JSONArray jSs) { int countryCount = jSs.length(); ArrayList<Message> msglist = new ArrayList<Message>(); Message s = null;//from w w w. ja v a 2s .c o m // Taking each country, parses and adds to list object for (int i = 0; i < countryCount; i++) { try { // Call getCountry with country JSON object to parse the country s = getMsg((JSONObject) jSs.get(i)); msglist.add(s); } catch (JSONException e) { e.printStackTrace(); } } return msglist; }
From source file:org.apache.oltu.oauth2.ext.dynamicreg.server.request.JSONHttpServletRequestWrapper.java
public Map<String, String[]> getParameterMap() { if (!bodyRead) { String body = readJsonBody(); final JSONTokener x = new JSONTokener(body); char c;/*from www. j av a 2 s. c om*/ String key; if (x.nextClean() != '{') { throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, a JSON object text must begin with '{'", body)); } for (;;) { c = x.nextClean(); switch (c) { case 0: throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, a JSON object text must end with '}'", body)); case '}': return Collections.unmodifiableMap(parameters); default: x.back(); key = x.nextValue().toString(); } /* * The key is followed by ':'. We will also tolerate '=' or '=>'. */ c = x.nextClean(); if (c == '=') { if (x.next() != '>') { x.back(); } } else if (c != ':') { throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, expected a ':' after the key '%s'", body, key)); } Object value = x.nextValue(); // guard from null values if (value != null) { if (value instanceof JSONArray) { // only plain simple arrays in this version JSONArray array = (JSONArray) value; String[] values = new String[array.length()]; for (int i = 0; i < array.length(); i++) { values[i] = String.valueOf(array.get(i)); } parameters.put(key, values); } else { parameters.put(key, new String[] { String.valueOf(value) }); } } /* * Pairs are separated by ','. We will also tolerate ';'. */ switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == '}') { return Collections.unmodifiableMap(parameters); } x.back(); break; case '}': return Collections.unmodifiableMap(parameters); default: throw new OAuthRuntimeException(format( "String '%s' is not a valid JSON object representation, Expected a ',' or '}", body)); } } } return Collections.unmodifiableMap(parameters); }
From source file:org.wso2.carbon.dataservices.core.description.query.QueryFactory.java
public static OMElement getJSONResultFromText(String jsonMapping) throws DataServiceFault { try {//ww w . j ava 2s.c o m OMElement resultEl = createElement(DBSFields.RESULT); JSONObject resultObj = new JSONObject(jsonMapping); String[] topLevelNames = JSONObject.getNames(resultObj); if (topLevelNames == null || topLevelNames.length != 1) { throw new DataServiceFault("There must be exactly 1 top level object in the JSON mapping, " + "found " + (topLevelNames != null ? topLevelNames.length : 0)); } String wrapperName = topLevelNames[0]; String rowName = null; Object rootObj = resultObj.get(wrapperName); if (rootObj instanceof JSONArray) { throw new DataServiceFault("The top level object cannot be an array"); } else if (rootObj instanceof JSONObject) { String[] secondLevelNames = JSONObject.getNames((JSONObject) rootObj); if (secondLevelNames.length == 1) { Object obj = ((JSONObject) rootObj).get(secondLevelNames[0]); if (obj instanceof JSONArray) { rowName = secondLevelNames[0]; JSONArray array = (JSONArray) obj; if (array.length() != 1) { throw new DataServiceFault( "The JSON array should be of size 1, found " + array.length()); } obj = array.get(0); if (!(obj instanceof JSONObject)) { throw new DataServiceFault("The JSON array element must be a JSON Object"); } addJSONMappingResultRecord((JSONObject) obj, resultEl); } else { addJSONMappingResultRecord((JSONObject) rootObj, resultEl); } } else { addJSONMappingResultRecord((JSONObject) rootObj, resultEl); } } else { throw new DataServiceFault("The top level object cannot be a simple type"); } resultEl.addAttribute(DBSFields.ELEMENT, wrapperName, null); if (rowName != null) { resultEl.addAttribute(DBSFields.ROW_NAME, rowName, null); } return resultEl; } catch (DataServiceFault e) { throw e; } catch (Exception e) { throw new DataServiceFault(e, "Error in parsing JSON result mapping: " + e.getMessage()); } }
From source file:com.tunyk.jsonbatchtranslate.JsonBatchTranslateDefaultImpl.java
private JSONArray iterateJSONArray(JSONArray json, String namespace, List<String> namespacesToInclude) throws JSONException { JSONArray res = new JSONArray(); if (namespace == null) { namespace = ""; }// ww w . j a v a 2s . co m for (int i = 0; i < json.length(); i++) { Object obj = json.get(i); String namespaceKey = namespace + "[]"; if (obj instanceof JSONObject) { JSONObject innerJson = iterateJSONObject((JSONObject) obj, namespaceKey, namespacesToInclude); res.put(i, innerJson); } else if (obj instanceof JSONArray) { JSONArray innerJson = iterateJSONArray((JSONArray) obj, namespaceKey, namespacesToInclude); res.put(i, innerJson); } else if ((obj instanceof String) && isNamespaceIncluded(namespaceKey, namespacesToInclude)) { Long id = getUID(); stringMap.put(id, obj.toString()); res.put(i, id); } else { res.put(i, obj); } } return res; }
From source file:com.tunyk.jsonbatchtranslate.JsonBatchTranslateDefaultImpl.java
private JSONArray reverseIterateJSONArray(JSONArray json) throws JSONException { JSONArray res = new JSONArray(); for (int i = 0; i < json.length(); i++) { Object obj = json.get(i); if (obj instanceof JSONObject) { JSONObject innerJson = reverseIterateJSONObject((JSONObject) obj); res.put(i, innerJson);//from w ww. jav a2 s . c om } else if (obj instanceof JSONArray) { JSONArray innerJson = reverseIterateJSONArray((JSONArray) obj); res.put(i, innerJson); } else if ((obj instanceof Long) && stringMap.containsKey(obj)) { res.put(i, stringMap.get(obj)); } else { res.put(i, obj); } } return res; }