private ResponseScratch parseResponseForMigration(String responseBody) { return new GsonBuilder().create().fromJson(responseBody, ResponseScratch.class); }
Map<String, String> myMap = new HashMap<String, String>(); myMap.put("one", "hello"); myMap.put("two", "world"); Gson gson = new GsonBuilder().create(); String json = gson.toJson(myMap); System.out.println(json); Type typeOfHashMap = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> newMap = gson.fromJson(json, typeOfHashMap); // This type must match TypeToken System.out.println(newMap.get("one")); System.out.println(newMap.get("two"));
Gson gson= new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create(); String date = "\"2013-02-10T13:45:30+0100\""; Date test = gson.fromJson(date, Date.class); System.out.println("date:" + test);
new GsonBuilder() .registerTypeAdapter(TestAnnotationBean.class, new AnnotatedDeserializer<TestAnnotationBean>()) .create(); TestAnnotationBean tab = gson.fromJson(json, TestAnnotationBean.class); System.out.println(tab.foo); System.out.println(tab.bar); tab = gson.fromJson(json, TestAnnotationBean.class); System.out.println(tab.foo); System.out.println(tab.bar); tab = gson.fromJson(json, TestAnnotationBean.class); System.out.println(tab.foo); System.out.println(tab.bar); T pojo = new Gson().fromJson(je, type);
public static void format(String json) { JsonObject jsonElement = new Gson().fromJson(json, JsonObject.class); JsonArray webServices = (JsonArray) jsonElement.get("webServices"); String webServicePath = webService.get("path").getAsString(); System.out.println("Excluding WS " + webServicePath + " from code generation"); continue; writeSourceFile(helper.packageInfoFile(webServicePath), applyTemplate("package-info.vm", webServiceContext)); for (JsonElement actionElement : (JsonArray) webService.get("actions")) { JsonObject action = (JsonObject) actionElement;
private String mockLongPollBody(String notificationsStr) { List<ApolloConfigNotification> oldNotifications = gson.fromJson(notificationsStr, notificationType); List<ApolloConfigNotification> newNotifications = new ArrayList<>(); for (ApolloConfigNotification notification : oldNotifications) { newNotifications .add(new ApolloConfigNotification(notification.getNamespaceName(), notification.getNotificationId() + 1)); } return gson.toJson(newNotifications); }
public List<HomeWizardSmartPlugDevice> getDevices() { List<HomeWizardSmartPlugDevice> homewizardDevices = new ArrayList<>(); try { String result = requestJson(EMPTY_STRING); JsonParser parser = new JsonParser(); JsonObject resultJson = parser.parse(result).getAsJsonObject(); cloudPlugId = resultJson.get("id").getAsString(); String all_devices_json = resultJson.get("devices").toString(); Device[] devices = gson.fromJson(all_devices_json, Device[].class); // Fix names from JSON for (Device device : devices) { device.setTypeName(StringUtils.capitalize(device.getTypeName().replace("_", " "))); homewizardDevices.add(mapDeviceToHomeWizardSmartPlugDevice(device)); } } catch(Exception e) { log.warn("Error while get devices from cloud service ", e); } log.info("Found: " + homewizardDevices.size() + " devices"); return homewizardDevices; }
@Override public List<WxMpUserSummary> getUserSummary(Date beginDate, Date endDate) throws WxErrorException { String url = "https://api.weixin.qq.com/datacube/getusersummary"; JsonObject param = new JsonObject(); param.addProperty("begin_date", SIMPLE_DATE_FORMAT.format(beginDate)); param.addProperty("end_date", SIMPLE_DATE_FORMAT.format(endDate)); String responseContent = post(url, param.toString()); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); return WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("list"), new TypeToken<List<WxMpUserSummary>>() { }.getType()); }
@NonNull public Command parse(@NonNull String json) .fromJson(json, ArchiveHabitsCommand.Record.class) .toCommand(habitList); .fromJson(json, ChangeHabitColorCommand.Record.class) .toCommand(habitList); .fromJson(json, CreateHabitCommand.Record.class) .toCommand(modelFactory, habitList); .fromJson(json, CreateRepetitionCommand.Record.class) .toCommand(habitList); .fromJson(json, DeleteHabitsCommand.Record.class) .toCommand(habitList); .fromJson(json, EditHabitCommand.Record.class) .toCommand(modelFactory, habitList); .fromJson(json, ToggleRepetitionCommand.Record.class) .toCommand(habitList); .fromJson(json, UnarchiveHabitsCommand.Record.class) .toCommand(habitList);
set(CONTAINER_NUMBER, String.valueOf(0)); } else { Gson gson = new Gson(); Map<String, Object> readLog = new TreeMap<>(); readLog = (Map) gson.fromJson(line, readLog.getClass()); int i = 0; int workeri = 0; }).create(); ConcurrentHashMap<String, Object> map = gson2.fromJson(cpuMetrics, type); if (map.size() > 0) { cpuMetricsFlag = true; if (containerMessage.get(AMParams.CONTAINER_ROLE).equals(XLearningConstants.WORKER) || containerMessage.get(AMParams.CONTAINER_ROLE).equals(XLearningConstants.EVALUATOR)) { set("workerCpuMemMetrics" + workeri, new Gson().toJson(map.get("CPUMEM"))); if (map.containsKey("CPUUTIL")) { set("workerCpuUtilMetrics" + workeri, new Gson().toJson(map.get("CPUUTIL"))); set("psCpuMemMetrics" + psi, new Gson().toJson(map.get("CPUMEM"))); if (map.containsKey("CPUUTIL")) { set("psCpuUtilMetrics" + psi, new Gson().toJson(map.get("CPUUTIL"))); Type type = new TypeToken<Map<String, List<Double>>>() { }.getType(); Map<String, List<Double>> map = new Gson().fromJson(cpuStatistics, type); if (map.size() > 0) { if (containerMessage.get(AMParams.CONTAINER_ROLE).equals(XLearningConstants.WORKER) || containerMessage.get(AMParams.CONTAINER_ROLE).equals(XLearningConstants.EVALUATOR)) {
public static MusicDetailInfo getInfo(final String id) { MusicDetailInfo info = null; try { JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.Song.songBaseInfo(id).trim()).get("result") .getAsJsonObject().get("items").getAsJsonArray().get(0).getAsJsonObject(); info = MainApplication.gsonInstance().fromJson(jsonObject, MusicDetailInfo.class); } catch (Exception e) { e.printStackTrace(); } return info; }
private static Set<SourceEntity> getSourceEntities(String response) { Set<SourceEntity> result = Sets.newHashSet(); JsonObject jsonObject = new Gson().fromJson(response, JsonObject.class).getAsJsonObject(); JsonArray array = jsonObject.getAsJsonArray("sobjects"); for (JsonElement element : array) { String sourceEntityName = element.getAsJsonObject().get("name").getAsString(); result.add(SourceEntity.fromSourceEntityName(sourceEntityName)); } return result; }
public WxMpMassNews deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMassNews wxMpMassNews = new WxMpMassNews(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("media_id") != null && !json.get("media_id").isJsonNull()) { JsonArray articles = json.getAsJsonArray("articles"); for (JsonElement article1 : articles) { JsonObject articleInfo = article1.getAsJsonObject(); WxMpMassNews.WxMpMassNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMassNews.WxMpMassNewsArticle.class); wxMpMassNews.addArticle(article); } } return wxMpMassNews; } }
@Test public void otherAttributes() { String json = "{\"a\":1,\"b\":\"B\",\"c\":true,\"d\":null}"; OtherAttributes o = gsonWithOptions.fromJson(json, OtherAttributes.class); check(o.rest().get("c")).is(new JsonPrimitive(true)); check(o.rest().get("d")).is(JsonNull.INSTANCE); check(gsonWithOptions.toJson(o)).is(json); }
public WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem wxMaterialNewsBatchGetNewsItem = new WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("media_id") != null && !json.get("media_id").isJsonNull()) { wxMaterialNewsBatchGetNewsItem.setMediaId(GsonHelper.getAsString(json.get("media_id"))); } if (json.get("update_time") != null && !json.get("update_time").isJsonNull()) { wxMaterialNewsBatchGetNewsItem.setUpdateTime(new Date(1000 * GsonHelper.getAsLong(json.get("update_time")))); } if (json.get("content") != null && !json.get("content").isJsonNull()) { JsonObject newsItem = json.getAsJsonObject("content"); wxMaterialNewsBatchGetNewsItem.setContent(WxMpGsonBuilder.create().fromJson(newsItem, WxMpMaterialNews.class)); } return wxMaterialNewsBatchGetNewsItem; } }