gson

GSON Expected BEGIN_ARRAY but was STRING at line 1 error

我怕爱的太早我们不能终老 提交于 2019-12-23 16:16:04
问题 I am trying to parse a String array using Retrofit and GSON which is returned from the API: This is what the response normally looks like (CASE 1): ["Scan finished, scan information embedded in this object", "https://www.virustotal.com/url/297c349554bdc7e2f09a85be309f08cb2f16a9174068bd5bc6e298ed90a5eed9/analysis/1485313628/", 8, 64] This is what the response looks like in a specific case (CASE 2): ["Scan request successfully queued, come back later for the report", "https://www.virustotal.com

Gson 序列化器的使用

爷,独闯天下 提交于 2019-12-23 12:53:53
业务场景:最近在做一个文件路径的加密,很多类设计到文件的路径,由于我们项目的使用的框架比较奇葩,然而很多类继承了一个处理返回参数的父类,我就在这个父类里面开始动刀了: 序列化这边的工具用到的是GSON属于谷歌: 1 import com.alibaba.fastjson.JSONObject; 2 import com.fasterxml.jackson.databind.ObjectMapper; 3 import com.google.gson.*; 4 import com.sxx.inpa.CommonEnctyptUtil; 5 6 import java.io.IOException; 7 import java.lang.reflect.Type; 8 import java.util.*; 9 10 public class GosnDemo { 11 12 public static void main(String[] args) throws IOException { 13 // GsonBuilder用来生成Gson对象. 规定Gson的序列化和返序列化时的格式等内容. 14 GsonBuilder gsonBuilder = new GsonBuilder(); 15 // 这里定义匿名内部类的序列化器

Convert Map<Integer, Object> to JSON with GSON?

孤街浪徒 提交于 2019-12-23 12:34:08
问题 Hee Guys, I'm curious if it is possible to convert a Map to JSON and vica versa with GSON? The object that i'm putting in is already converted to a Object from JSON with GSON. Object that i'm using looks like this: public class Locations{ private List<Location> location; <-- Getter / Setter --> public class Location{ <-- Fields and Getters/Setters --> } } 回答1: Assuming you're using a java.util.Map : Map<Integer, Object> map = new HashMap<>(); map.put(1, "object"); // Map to JSON Gson gson =

Retrofit image upload using Base64 in android

烈酒焚心 提交于 2019-12-23 09:34:50
问题 I am trying to upload the image using retrofit in Base64 format. To convert bitmap to Base64, public static String convertImageToStringForServer(Bitmap imageBitmap){ ByteArrayOutputStream stream = new ByteArrayOutputStream(); if(imageBitmap != null) { imageBitmap.compress(Bitmap.CompressFormat.JPEG, 60, stream); byte[] byteArray = stream.toByteArray(); return Base64.encodeToString(byteArray, Base64.DEFAULT); }else{ return null; } } I do not want to upload the image using Typedfile. My request

using GSON to parse and place into a list of objects

ε祈祈猫儿з 提交于 2019-12-23 09:27:28
问题 I found this article: http://www.dev-articles.com/article/Google-gson-and-list-of-objects-386003 It looks like it is trying to do what I want, which is parse my json and place them into a List of objects. What I dont get, or maybe the article is missing something, is how the "Project" class gets used. It seems it comes out of nowhere. EDIT: Thanks to yorkw I now have: public static String parseJSONResponse(String jsonResponse) { Type listType = new TypeToken<List<SingleEvent>>(){}.getType();

GSON Deserialize String or String Array

泪湿孤枕 提交于 2019-12-23 09:05:18
问题 I've had some trouble deserializing an object that contains 0 to many child objects that can either contains a string or a string array for a particular value. Here's an example JSON { "name": "process name", "tasks": [{ "name": "task 1", "fields": [{ "name": "field 1", "value": "123abc" }, { "name": "field 2", "value": ["value 1", "value 2"] } ] }, { "name": "task 2", "fields": [] }] } I have a Java entity setup to match this structure like this: public class Process { public Process() {}

gson

馋奶兔 提交于 2019-12-23 05:55:58
Factory method 'gsonBuilder' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; 升级一下版本从2.2.4到2.6.2就可以了 <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> 来源: CSDN 作者: weixin_41630573 链接: https://blog.csdn.net/weixin_41630573/article/details/103656566

Gson parser class for Json response - Android

非 Y 不嫁゛ 提交于 2019-12-23 05:46:11
问题 i've got response like below, Sometime the language tag come with array value { "name": "Tony", "id":"123", "language":["en","fr" ] } for some user the language value come with string value. { "name": "Max", "id":"124", "language":"en" } How to create parser class to parse the json response using GSON? 回答1: Conceptually this is not a valid JSON. The type of "language" should be determined for the very first moment. If you are sending a list of languages in some cases, it is easy to send a

Error when adding Gson library external Jar

半腔热情 提交于 2019-12-23 05:16:17
问题 i would like to use the GSON library, a Java library that can be used to convert Java Objects into their JSON representation but it not works. Properties -> Java Build Path -> Libraries -> Add External JARs (add the downloaded gson jar) I get this error: caused by: java.lang.NoClassDefFoundError: com.google.gson.GsonBuilder Help 回答1: Try creating a folder call 'lib' under your project and copy the gson jar over to 'lib' directory and repeat your step above. 来源: https://stackoverflow.com

Gson: How do I deserialize interface fields having class names?

给你一囗甜甜゛ 提交于 2019-12-23 05:08:53
问题 I am trying to deserialize a List of Research objects but I can't make it work. I know that I need a custom adapter to deserialize my object since I am using an interface in my Research class but I am unsure on how to implement it. I currently have a serializer which seems to work and saves the class type for desirialization. I've been using bits of code from this SO post to make the serializer: Gson deserialize interface to its Class implementation This is the JSON I'm working with: [ {