gson

Parse JSON using gson with key name containing period (.)

风格不统一 提交于 2020-07-31 03:58:07
问题 { "data.url" : "http://dev.com", "value": [ { "color": "red" "shape": "rect" }, { "color": "blue" "shape": "rect" } ] } Tried using gson.json, but . character is blocking from creating class, Is there a way to remap the dotted field? 回答1: Because Java doesn't allow . in a variable name, you need to use the @SerializedName annotation on that field in your class: public class MyPojo { @SerializedName("data.url") private String dataUrl; ... } 来源: https://stackoverflow.com/questions/21274183

Java 设置Excel单元格格式—基于Spire.Cloud.SDK for Java

删除回忆录丶 提交于 2020-07-28 07:47:35
本文介绍使用Spire.Cloud.SDK for Java来设置Excel单元格格式,包括字体、字号、单元格背景、字体下滑线、字体加粗、字体倾斜、字体颜色、单元格对齐方式、单元格边框等 一、下载SDK 及导入jar 1. 下载 地址 2. 下载后,创建Maven项目程序(程序使用的IDEA,如果是Eclipse,参照这里的 方法 ),并在pom.xml文件中配置 Maven 仓库路径: < repositories > < repository > < id > com.e-iceblue </ id > < name > cloud </ name > < url > http://repo.e-iceblue.cn/repository/maven-public/ </ url > </ repository > </ repositories > 在 pom.xml 文件中指定 Spire.cloud.sdk的 Maven 依赖: < dependencies > < dependency > < groupId > cloud </ groupId > < artifactId > spire.cloud.sdk </ artifactId > < version > 3.5.0 </ version > </ dependency > < dependency > <

javascript WebUploader 分片上传

孤者浪人 提交于 2020-07-27 02:00:52
1,项目调研 因为需要研究下断点上传的问题。找了很久终于找到一个比较好的项目。 在GoogleCode上面,代码弄下来超级不方便,还是配置hosts才好,把代码重新上传到了github上面。 https://github.com/freewebsys/java-large-file-uploader-demo 效果: 上传中,显示进度,时间,百分比。 点击【Pause】暂停,点击【Resume】继续。 2,代码分析 原始项目: https://code.google.com/p/java-large-file-uploader/ 这个项目最后更新的时间是 2012 年,项目进行了封装使用最简单的方法实现了http的断点上传。 因为html5 里面有读取文件分割文件的类库,所以才可以支持断点上传,所以这个只能在html5 支持的浏览器上面展示。 同时,在js 和 java 同时使用 cr32 进行文件块的校验,保证数据上传正确。 代码在使用了最新的servlet 3.0 的api,使用了异步执行,监听等方法。 上传类UploadServlet @Component("javaLargeFileUploaderServlet") @WebServlet(name = "javaLargeFileUploaderServlet", urlPatterns = { "

Gson 用于Java POJO与字符串序列化和反序列化

落花浮王杯 提交于 2020-07-25 06:26:21
1)pom.xml <dependencies> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> </dependencies> 2)ProtoMgr.java package gson; import com.google.gson.Gson; public class ProtoMgr { private static final Gson gson = new Gson(); public static String toJson(Object obj) { return gson.toJson(obj); } public static <T> T fromJson(String json, Class<T> clazz) { return gson.fromJson(json, clazz); } } 3)POJO RankItem.java package gson; public class RankItem { public int

Default value for null fields using GSON

。_饼干妹妹 提交于 2020-07-22 09:19:41
问题 I would like to set default values for some fields on my model, so if during deserialization my json has missing fields, those default values are used. What I would like to understand which is the best practice to do this, and why. I should add the control of null in the setters of my model, I should use the try catch to find any cases, I should extend and modify typeAdapter (this solution seems very verbose to me, let me know if I'm wrong) or there is a even better solution? 回答1: If those

Default value for null fields using GSON

余生颓废 提交于 2020-07-22 09:19:35
问题 I would like to set default values for some fields on my model, so if during deserialization my json has missing fields, those default values are used. What I would like to understand which is the best practice to do this, and why. I should add the control of null in the setters of my model, I should use the try catch to find any cases, I should extend and modify typeAdapter (this solution seems very verbose to me, let me know if I'm wrong) or there is a even better solution? 回答1: If those

How can I get Gson to deserialize an interface type? [duplicate]

寵の児 提交于 2020-07-18 22:17:31
问题 This question already has answers here : How to handle deserializing with polymorphism? (4 answers) Closed 4 years ago . I have an interface public interace ABC { } Implementation of this is as follows: public class XYZ implements ABC { private Map<String, String> mapValue; public void setMapValue( Map<String, String> mapValue) { this.mapValue = mapValue; } public Map<String, String> getMapValue() { return this.mapValue } } I want to deserialize a class using Gson which is implemented as

Out of memory exception in gson.fromJson()

天大地大妈咪最大 提交于 2020-07-15 04:43:16
问题 I use the following code for converting Json string(strWebserviceResult) to my Object: EntMyClass entMyClass = gson.fromJson(strWebserviceResult,EntMyClass.class); When strWebserviceResult is large (about 2.5 MB) I get the Out of memory exception on this line on Android phone devices not in Tablet that has larger memory. How can I solve that. Does anybody have any suggestion? 05-26 15:52:49.607: E/dalvikvm-heap(2078): Out of memory on a 9200-byte allocation. 05-26 15:52:49.618: E/dalvikvm

How to throw exception while converting from JsonStr to PojoClassObj if any key/value is missing in the JsonStr?

僤鯓⒐⒋嵵緔 提交于 2020-07-07 13:27:28
问题 I tried using @NotNull annotation but it did not work. If any field is missing in the JsonString , it gives null(String) or zero(int) . But what I want is if any field is missing in the JsonStr defined in the class should throw an exception. Add: My PojoClass may have object reference or multilevel object reference. I am using Gson for the conversion of the String to obj. for more clarification, I have added my code below: JsonStr: { "name":"John", "id":1, "roll":100, "c":{ "city":"Dhaka",

Same field has two different types gives trouble with Gson converter for Retrofit 2

烈酒焚心 提交于 2020-07-06 10:39:49
问题 Here is the json schema: As you can see, rated can be both boolean and object. I am using Retrofit 2 and Gson converter. How should I create my model for this schema? 回答1: Here's how I solved this issue: Create a custom type adapter in your model and parse rated manually; public class AccountState { //@SerializedName("rated") //NOPE, parse it manually private Integer mRated; //also don't name it rated public Integer getRated() { return mRated; } public void setRated(Integer rated) { this