gson

Gson: Expected a string but was BEGIN_OBJECT

谁都会走 提交于 2019-12-12 08:20:58
问题 I am trying to read a simple JSON response. { "response": "ok" } Here is my code: JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); String response = null; boolean success = false; reader.beginObject(); if (reader.hasNext()) { String token = reader.nextName(); if (token.equals("response")) { response = reader.nextString(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); But I am getting this error: Expected a string but was BEGIN_OBJECT I don't

How do I parse this JSON string using GSON in servlet

妖精的绣舞 提交于 2019-12-12 08:16:29
问题 How can I parse this JSON string to create collection object in servlet {"title":["1","2"],"amount":["1","3"]} inner class in my servlet public class Data { private List<String> title; private List<String> amount; //getters and setters } parsing json Gson gson = new Gson(); String param=request.getParameter("info"); Data data = gson.fromJson(param, Data.class); List<String> a=data.getTitle(); if(a==null){p("a null");}else{p("a not null");} //here a is null, prints "a null" here is the

Convert ArrayList<JsonArray> to float[][] in Java

梦想的初衷 提交于 2019-12-12 06:37:07
问题 What is best and fastest way to convert an ArrayList<JsonArray> to float[][] using Gson? The ArrayList <JsonArray> is a 2D array of long with this sample format: [ [ -0.0028871582, -0.0017856462, 0.0078000603, 0.003144495, 0.0042561297, -0.026877755, 0.019066211, 0.050337251, -0.00062063418], [ -0.6545645087, 0.7474752828, 1.8797838739, 0.287287200, 0.0007858753, -0.742472785, 0.019066211, 0.050337251, -0.00062063418], ... ] People say I should scan each item with two go and do the conversion

Java List of parameterized generic type creates different type of objects

折月煮酒 提交于 2019-12-12 05:44:25
问题 I have a function that needs to create a list of objects of a type that is passed in as a generic. public static <T> List<T> readJsonFile(final String filePath) { List<T> objectsFromFile=null; File file = new File(System.getProperty("catalina.base") + filePath); Gson gson = new Gson(); try (FileInputStream JSON = new FileInputStream(file)) { BufferedReader br = new BufferedReader(new InputStreamReader(JSON)); objectsFromFile = gson.fromJson(br, new TypeToken<List<T>>() { }.getType()); } catch

How to parse a json array with an array of arrays

邮差的信 提交于 2019-12-12 05:38:40
问题 I think I am missing something here on the parsing of this json object. The output I receive is in this format: { "columns" : [ "type", "relation" ], "data" : [ [ "SOURCE", { "paged_traverse" : "localhost/db/data/node/26/paged/traverse/{returnType}{?pageSize,leaseTime}", "outgoing_relationships" : "localhost/db/data/node/26/relationships/out", "data" : { "DATUM" : "December" }, "all_typed_relationships" : "localhost/db/data/node/26/relationships/all/{-list|&|types}", "traverse" : "localhost

Gson class not found when running android app

☆樱花仙子☆ 提交于 2019-12-12 05:24:51
问题 I have some problems about Gson import in my project if I dependency like this: compile 'com.google.code.gson:gson:2.8.1' The compile is OK,but when I run my app, it has bean crashed,such as enter image description here I try to dump the apk and analyze it, I found there is Gson in the dex,but runtime crash. but when I download the gson.jar and put it into libs then make the dependency like this: compile fileTree(dir: 'libs', include: ['*.jar']) it fully worked. and I don't know why ? so help

Android Retrofit GET request ConversionException issue

a 夏天 提交于 2019-12-12 05:24:40
问题 I'm using Retrofit to make REST requests and create the corresponding model objects (with gson using the @SerializedName annotation). There is one particular GET request that occasionally causes a ConversionException though, and I am having trouble tracking down the cause. I will make 10-20 similar GET requests nearly at the same time with varying parameters and about 90% of them return correctly. The remaining fail with a ConversionException: retrofit.converter.ConversionException: com

“java.nio.charset.UnsupportedCharsetException: utf8mb4” error in retrofit

你。 提交于 2019-12-12 05:02:21
问题 10-26 23:29:25.422 30062-30062/okok.com.retrofittrial W/System.err: java.nio.charset.UnsupportedCharsetException: utf8mb4 10-26 23:29:25.422 30062-30062/okok.com.retrofittrial W/System.err: at java.nio.charset.Charset.forName(Charset.java:571) 10-26 23:29:25.422 30062-30062/okok.com.retrofittrial W/System.err: at okhttp3.MediaType.charset(MediaType.java:103) 10-26 23:29:25.422 30062-30062/okok.com.retrofittrial W/System.err: at okhttp3.ResponseBody.charset(ResponseBody.java:114) 10-26 23:29

GSON Custom parsing

南楼画角 提交于 2019-12-12 04:55:48
问题 Hi I'm writing an android App that interfaces with an external webservice that outputs JSON formatted info. I'm using GSON to generate POJOs for the output of the web service, but I'm having a problem with this entity: player: { 1: { number: "6", name: "Joleon Lescott", pos: "D", id: "2873" }, 2: { number: "11", name: "Chris Brunt", pos: "D", id: "15512" }, 3: { number: "23", name: "Gareth McAuley", pos: "D", id: "15703" } } Using a service like http://www.jsonschema2pojo.org/ I was able to

Why am I getting a JsonSyntaxException: Expected BEGIN_ARRAY but was STRING here?

允我心安 提交于 2019-12-12 04:48:57
问题 I have one JSON file: { "settings": { "header": { "id": "240A64CDC43F", "filename": "network", "sequence": "0", "last_modified": "2015-04-21T16:33", "JSON_file_version": "2.1" }, "data": { "engine_config": { "bed_name": "IdealTempCDC43F", "provisioned": "false", "connected": "false", "IP_address": "192.168.10.1", "connection_error": "None" }, "networks": { "available": "7", "SSIDs": { "wireless_1": { "SSID": "$$ASI_WIFI$$", "mac_address": "A0:EC:F9:11:35:04", "channel": "11", "RSSI": "-64dBm"