gson

Parsing JSON in JAVA usng Gson

自古美人都是妖i 提交于 2019-12-25 12:14:58
问题 So I am trying to parse a JSON object in Java using Gson. I am trying to consume an API from a website. This is the ticker: https://api.coinmarketcap.com/v1/ticker/bitcoin/ The JSON looks like this: [ { "id": "bitcoin", "name": "Bitcoin", "symbol": "BTC", "rank": "1", "price_usd": "3591.95", "price_btc": "1.0", "24h_volume_usd": "3263990000.0", "market_cap_usd": "59516499433.0", "available_supply": "16569412.0", "total_supply": "16569412.0", "percent_change_1h": "1.02", "percent_change_24h":

Parsing JSON in JAVA usng Gson

守給你的承諾、 提交于 2019-12-25 12:14:50
问题 So I am trying to parse a JSON object in Java using Gson. I am trying to consume an API from a website. This is the ticker: https://api.coinmarketcap.com/v1/ticker/bitcoin/ The JSON looks like this: [ { "id": "bitcoin", "name": "Bitcoin", "symbol": "BTC", "rank": "1", "price_usd": "3591.95", "price_btc": "1.0", "24h_volume_usd": "3263990000.0", "market_cap_usd": "59516499433.0", "available_supply": "16569412.0", "total_supply": "16569412.0", "percent_change_1h": "1.02", "percent_change_24h":

Parsing generic ArrayList using GSON

删除回忆录丶 提交于 2019-12-25 08:57:20
问题 I'm having some trouble parsing a generic ArrayList with GSON. I've confirmed that this method works if the specific type for the ArrayList is specified (using no generics), but I'd like to write a method which works with generics. Based on other stack overflow's questions, this should work, and I can't really understand why it doesn't. It fails to parse the ArrayList so all I get is a collection of LinkedTreeMap . The generic method is below: public <T> List<T> listFromJson(@NonNull Class<T>

JSON parse error using gson

情到浓时终转凉″ 提交于 2019-12-25 08:15:44
问题 I am trying to parse the data into a class. But the problem is that one of the values is in Json format. Hence I am getting a MalformedJsonException. This is the Json string: { "name": "Check on Server Health ", "state": "ABORTED", "startTime": 1332962596131, "triggeredBy": "GUI_MANUAL", "completionPct": 25, "currentStep": "sayHello", "processDefId": "SW21SW", "jobRetries": 0, "businessKey": -1, "comments": "couldn't instantiate class com.mikewidgets.helloWorld.HelloWorldTask", "endTime":

hashmap to JSON using GSON

爷,独闯天下 提交于 2019-12-25 07:26:58
问题 I am using Gson for converting my Java Objects to GSON. I wanted to convert my HashMap to JSON. Assuming my Object1 has structure: public class Object1 { String a; String b; String c; String toString() { return "a: " + a + " b: " + b + " c: " + c; } } and my Object2 has Structure: public class Object2 { String e; String f; } I wanted my final JSON to look like { { "a" : "aValue", "b" : "bValue", "c" : "cValue" } : { "e" : "eValue", "f" : "fValue" } } Bit I am getting it as { "a: aValue b:

How to post json object using retrofit

瘦欲@ 提交于 2019-12-25 06:35:38
问题 i wanted to post the json object using retrofit. i have created following interface: public interface syncinter { @POST("url") void sync_data(@Body JSONObject ordObj, Callback<JsonObject> callback); } the following data i want it to post. final JSONObject ordJsonObj = new JSONObject(); JSONArray ordJsonArray = new JSONArray(); try { ordJsonObj.put("Order_Nos",mOrdArr.size()); for (int i=0; i<mOrdArr.size();i++) { JSONObject ordObj = new JSONObject(); ordObj.put("Order_No",mOrdArr.get(i)

Cannot resolve method 'fromJson(org.json.JSONObject, java.lang.Class<me.myrottentomatoes.Result[]>)'

早过忘川 提交于 2019-12-25 05:46:08
问题 in the method fromJson, in my class Result, I want to deserialize the JsonObject into an ArrayList of the type Result. public static ArrayList<Result> fromJson(JSONObject items) { ArrayList results = new ArrayList<Result>(); Gson gson = new Gson(); return results = gson.fromJson(items, Result[].class); } I can't find the solution for this error: Cannot resolve method 'fromJson(org.json.JSONObject, java.lang.Class<me.myrottentomatoes.Result[]>)' To me it looks like I'm passing the right

Gson : Is there a way to preserve type information if serialization/deserialization target is an Object

为君一笑 提交于 2019-12-25 05:32:40
问题 I have the following code import com.google.gson.Gson; /** * * @author yccheok */ public class JavaApplication18 { public static class Holder { public Object value; } /** * @param args the command line arguments */ public static void main(String[] args) { Gson gson = new Gson(); Integer i = new Integer(123); Holder holder = new Holder(); holder.value = i; String json = gson.toJson(holder); System.out.println(json); Holder newHolder = gson.fromJson(json, Holder.class); System.out.println

parsing JSON array gives error

一笑奈何 提交于 2019-12-25 05:15:19
问题 I have the following Javascript object: var o = { "username":"username", "args": [ "1", "2", "3" ] }; And send it like: xhr.send(JSON.stringify(o)); My java class: public class Command implements Serializable { private String username; private String[] args; //getters, setters constructors etc. } And in my servlet: @Override public void doPost(HttpServletRequest request, HttpServletResponse response){ Command c; try { c = gson.fromJson(request.getReader(), Command.class); } catch(Exception e)

How to set Locale on GSON (for decimal number separators)

最后都变了- 提交于 2019-12-25 05:13:31
问题 I know you can set date formatting, but how do you set other locale specific stuff like decimal number formatting? (I mean comma vs. dot) 回答1: Short answer: you don't. You seem to be somewhat confusing the textual representation of a number and an actual numeric value. While JSON is a text-based data interchange format, a number (vs. a string) in JSON is a numeric value and is not affected by locale. Section 2.4 of the JSON specification provides the specific definition (emphasis mine): 2.4.