static Map<String, String> parseResponse(String jsonResponseString) throws IOException { // Parse Json Map<String, String> responseMap = new HashMap<>(); if (StringUtils.isNotBlank(jsonResponseString)) { JsonObject jsonObject = new JsonParser().parse(jsonResponseString).getAsJsonObject(); // Handle error if any handleResponseError(jsonObject); // Get all responseKeys for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { responseMap.put(entry.getKey(), entry.getValue().toString().replaceAll("\"", "")); } } return responseMap; }
public BulkResultItem(String operation, JsonObject values) { this.operation = operation; this.index = values.get("_index").getAsString(); this.type = values.get("_type").getAsString(); this.id = values.has("_id") && !values.get("_id").isJsonNull() ? values.get("_id").getAsString() : null; this.status = values.get("status").getAsInt(); this.error = values.has("error") ? values.get("error").toString() : null; if (values.has("error") && values.get("error").isJsonObject()) { final JsonObject errorObject = values.get("error").getAsJsonObject(); this.errorType = errorObject.has("type") ? errorObject.get("type").getAsString() : null; this.errorReason = errorObject.has("reason") ? errorObject.get("reason").getAsString() : null; } else { this.errorType = null; this.errorReason = null; } this.version = values.has("_version") ? values.get("_version").getAsInt() : null; }
@Override protected Void doInBackground(Void... params) { try { JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.Billboard.billSongList(BILLBOARD_KING, 0, 3)); JsonArray array = jsonObject.get("song_list").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { BillboardInfo billboardInfo = new BillboardInfo(); billboardInfo.title = array.get(i).getAsJsonObject().get("title").toString(); billboardInfo.author = array.get(i).getAsJsonObject().get("author").toString(); billboardInfo.id = array.get(i).getAsJsonObject().get("artist_id").toString(); items.add(billboardInfo); } } catch (NullPointerException e) { e.printStackTrace(); } return null; }
public static String getKernelInfoFromElasticSearchBySource( String instanceId, String kernelInfoApi, String source) throws Exception { JsonArray hits; JsonParser parser = new JsonParser(); String kernelVersion = null; Map<String, Object> mustFilter = new HashMap<>(); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); Map<String, Object> mustTermsFilter = new HashMap<>(); mustFilter.put(convertAttributetoKeyword(PacmanRuleConstants.RESOURCE_ID),instanceId); mustFilter.put(convertAttributetoKeyword(PacmanRuleConstants.SOURCE_FIELD),source); JsonObject resultJson = RulesElasticSearchRepositoryUtil.getQueryDetailsFromES(kernelInfoApi, mustFilter,mustNotFilter, shouldFilter, null, 0, mustTermsFilter,null); if (null != resultJson && resultJson.has(PacmanRuleConstants.HITS)) { String hitsJsonString = resultJson.get(PacmanRuleConstants.HITS).toString(); JsonObject hitsJson = (JsonObject) parser.parse(hitsJsonString); hits = hitsJson.getAsJsonObject().get(PacmanRuleConstants.HITS).getAsJsonArray(); if (hits.size() > 0) { JsonObject firstObject = (JsonObject) hits.get(0); JsonObject sourceJson = (JsonObject) firstObject.get(PacmanRuleConstants.SOURCE); if (null != sourceJson && sourceJson.has(PacmanRuleConstants.KERNEL_FIELD)) { kernelVersion = sourceJson.get(PacmanRuleConstants.KERNEL_FIELD).getAsString(); } } } return kernelVersion; }
private static void buildTokenizerFactory(JsonElement map, String analyzerName, CustomAnalyzer.Builder builder) throws IOException { if (!(map instanceof JsonObject)) { throw new IllegalArgumentException("Expecting a map with \"factory\" string and " + "\"params\" map in tokenizer factory;"+ " not: "+map.toString() + " in "+analyzerName); } JsonElement factoryEl = ((JsonObject)map).get(FACTORY); if (factoryEl == null || ! factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException("Expecting value for factory in char filter factory builder in:"+ analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.startsWith("oala.") ? factoryName.replaceFirst("oala.", "org.apache.lucene.analysis.") : factoryName; JsonElement paramsEl = ((JsonObject)map).get(PARAMS); Map<String, String> params = mapify(paramsEl); builder.withTokenizer(factoryName, params); }
static JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(json); Set<Map.Entry<String, JsonElement>> set = object.entrySet(); Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator(); if (value.isJsonObject()) { map.put(key, createHashMapFromJsonString(value.toString())); } else if (value.isJsonArray() && value.toString().contains(":")) { JsonArray array = value.getAsJsonArray(); if (null != array) { for (JsonElement element : array) { list.add(createHashMapFromJsonString(element.toString())); } else if (value.isJsonArray() && !value.toString().contains(":")) { map.put(key, value.getAsJsonArray()); map.put(key, value.getAsString());
private void setPartitionJsonValue(JsonArray partitionArray) { for (int m = 0; m < partitionArray.size(); m++) { JsonObject partitionObj = partitionArray.get(m).getAsJsonObject(); partitionWithText.add(partitionObj.get("Partition_with_Text").toString()); partitionText.add(partitionObj.get("Partition_Text").toString()); JsonArray partitionWith = partitionObj.getAsJsonArray("PartitionWith"); JsonObject partitionWithObj = partitionWith.get(0).getAsJsonObject(); partitionWithStream.add(partitionWithObj.get("Partition_Stream").toString()); partitionWithAttribute.add(partitionWithObj.get("attribute").getAsJsonArray()); partitionWithCondition.add(partitionWithObj.get("condition").getAsJsonArray()); for (int j = 0; j < partitionObj.get("Query_size").getAsInt(); j++) { JsonArray partitionQuery_Array = partitionObj.getAsJsonArray("Query_" + j); JsonObject partitionQuery_Obj = partitionQuery_Array.get(0).getAsJsonObject(); JsonArray queryArray = partitionQuery_Obj.getAsJsonObject().getAsJsonArray("Query"); setQueryJsonValue(queryArray, partitionObj.get("Partition_with_Text").toString()); } } }
JsonParser parser = new JsonParser(); JsonArray jsonArray = (JsonArray) parser.parse(params); try { Object[] result = new Object[jsonArray.size()]; result[i] = parsePB(element.toString(), pbMessage); } else {
Integer targetBri,Integer targetBriInc, ColorData colorData, DeviceDescriptor device, String body) { String theReturn = null; log.debug("executing HUE api request to send message to HomeAssistant: " + anItem.getItem().toString()); if(!validHass) { log.warn("Should not get here, no HomeAssistant clients configured"); if(anItem.getItem().isJsonObject()) hassCommand = aGsonHandler.fromJson(anItem.getItem(), HassCommand.class); else hassCommand = aGsonHandler.fromJson(anItem.getItem().getAsString(), HassCommand.class); hassCommand.setBri(BrightnessDecode.replaceIntensityValue(hassCommand.getBri(), BrightnessDecode.calculateIntensity(intensity, targetBri, targetBriInc), false));
public WxError deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxError wxError = new WxError(); JsonObject wxErrorJsonObject = json.getAsJsonObject(); if (wxErrorJsonObject.get("errcode") != null && !wxErrorJsonObject.get("errcode").isJsonNull()) { wxError.setErrorCode(GsonHelper.getAsPrimitiveInt(wxErrorJsonObject.get("errcode"))); } if (wxErrorJsonObject.get("errmsg") != null && !wxErrorJsonObject.get("errmsg").isJsonNull()) { wxError.setErrorMsg(GsonHelper.getAsString(wxErrorJsonObject.get("errmsg"))); } wxError.setJson(json.toString()); return wxError; }
if (data.isJsonObject()) { final Document doc = new Document(); for (final Entry<String, JsonElement> entry : data.getAsJsonObject().entrySet()) { final String key = entry.getKey(); final JsonElement element = entry.getValue(); final JsonArray array = data.getAsJsonArray(); for (final Iterator<JsonElement> iterator = array.iterator(); iterator.hasNext();) { final JsonElement type = iterator.next(); throw new JsonParseException("unable to parse json for key " + data.toString());
String jsonStr = "your json string "; Gson gson = new Gson(); JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject(); JsonElement elem = jsonObj.get("note"); if(elem.isJsonArray()) { //**Array** List<Note> notelist = gson.fromJson(elem.toString(), new TypeToken<List<Note>>(){}.getType()); } else if(elem.isJsonObject()) { //**Object** Note note = gson.fromJson(elem.toString(), Note.class); } else { //**String** String note = elem.toString(); }
@Override public void load(Reader reader) { try { JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject(); for (Map.Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) { if (jsonEntry.getKey().equals(DESCRIPTION_PROPERTY_NAME)) { continue; } SimpleUri id = new SimpleUri(jsonEntry.getKey()); String valueJson = jsonEntry.getValue().toString(); temporarilyParkedSettings.put(id, valueJson); } } catch (Exception e) { throw new RuntimeException("Error parsing config file!"); } } }
if (!jsonElement.isJsonObject()) throw new DataConversionException("Expecting json element " + jsonElement.toString() + " to be of type JsonObject."); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement keyValueElement = jsonObject.get(keyField); String keyString; try { keyString = keyValueElement.getAsString(); throw new DataConversionException("Could not get the key " + keyValueElement.toString() + " as a string", e);
@Override protected Void doInBackground(Integer... params) { JsonArray array = null; try { JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.Billboard.billSongList(params[0], 0, 3)); array = jsonObject.get("song_list").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { BillboardInfo billboardInfo = new BillboardInfo(); billboardInfo.title = array.get(i).getAsJsonObject().get("title").toString(); billboardInfo.author = array.get(i).getAsJsonObject().get("author").toString(); billboardInfo.id = array.get(i).getAsJsonObject().get("artist_id").toString(); items.add(billboardInfo); } } catch (NullPointerException e) { e.printStackTrace(); } return null; }
private static void buildCharFilters(JsonElement el, String analyzerName, CustomAnalyzer.Builder builder) throws IOException { if (el == null || el.isJsonNull()) { return; } if (! el.isJsonArray()) { throw new IllegalArgumentException("Expecting array for charfilters, but got:"+el.toString() + " for "+analyzerName); } JsonArray jsonArray = (JsonArray)el; List<CharFilterFactory> ret = new LinkedList<CharFilterFactory>(); for (JsonElement filterMap : jsonArray) { if (!(filterMap instanceof JsonObject)) { throw new IllegalArgumentException("Expecting a map with \"factory\" string and \"params\" map in char filter factory;"+ " not: "+filterMap.toString() + " in "+analyzerName); } JsonElement factoryEl = ((JsonObject)filterMap).get(FACTORY); if (factoryEl == null || ! factoryEl.isJsonPrimitive()) { throw new IllegalArgumentException( "Expecting value for factory in char filter factory builder in:"+analyzerName); } String factoryName = factoryEl.getAsString(); factoryName = factoryName.replaceAll("oala.", "org.apache.lucene.analysis."); JsonElement paramsEl = ((JsonObject)filterMap).get(PARAMS); Map<String, String> params = mapify(paramsEl); builder.addCharFilter(factoryName, params); } }
/** * {@inheritDoc} */ @Override public void selectAsset(ResourceUrn urn) { boolean isLoaded = assetManager.isLoaded(urn, UISkin.class); Optional<UISkin> asset = assetManager.getAsset(urn, UISkin.class); if (asset.isPresent()) { UISkin skin = asset.get(); if (!isLoaded) { asset.get().dispose(); } AssetDataFile source = skin.getSource(); String content = null; try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) { reader.setLenient(true); content = new JsonParser().parse(reader).toString(); } catch (IOException e) { logger.error(String.format("Could not load asset source file for %s", urn.toString()), e); } if (content != null) { JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content)); selectedAssetPending = urn.toString(); resetState(node); } } }
float aBriValue; float theValue; log.debug("executing HUE api request to send message to LifxDevice: " + anItem.getItem().toString()); if(!validLifx) { log.warn("Should not get here, no LifxDevice clients configured"); if(anItem.getItem().isJsonObject()) lifxCommand = aGsonHandler.fromJson(anItem.getItem(), LifxEntry.class); else lifxCommand = aGsonHandler.fromJson(anItem.getItem().getAsString(), LifxEntry.class); LifxDevice theDevice = getLifxDevice(lifxCommand.getName()); if (theDevice == null) {
public WxMpSemanticQueryResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WxMpSemanticQueryResult result = new WxMpSemanticQueryResult(); JsonObject resultJsonObject = json.getAsJsonObject(); if (GsonHelper.getString(resultJsonObject, "query") != null) { result.setQuery(GsonHelper.getString(resultJsonObject, "query")); } if (GsonHelper.getString(resultJsonObject, "type") != null) { result.setType(GsonHelper.getString(resultJsonObject, "type")); } if (resultJsonObject.get("semantic") != null) { result.setSemantic(resultJsonObject.get("semantic").toString()); } if (resultJsonObject.get("result") != null) { result.setResult(resultJsonObject.get("result").toString()); } if (GsonHelper.getString(resultJsonObject, "answer") != null) { result.setAnswer(GsonHelper.getString(resultJsonObject, "answer")); } if (GsonHelper.getString(resultJsonObject, "text") != null) { result.setText(GsonHelper.getString(resultJsonObject, "text")); } return result; }
public Sexp buildSexp(JsonElement js) { if (js.isJsonArray()) { return processJsonArray(js.getAsJsonArray()); } else if (js.isJsonObject()){ return processJsonObject(js.getAsJsonObject()); } else if (js.isJsonPrimitive()) { return processJsonPrimitive(js.getAsJsonPrimitive()); } else if (js.isJsonNull()) { return Sexp.nil; } throw new RuntimeException(format("<%s> cannot be handled by Logias processor.", js.toString())); }