gson

how to parse a huge JSON file without loading it in memory

拟墨画扇 提交于 2019-12-29 01:50:08
问题 I have a large JSON file (2.5MB) containing about 80000 lines. It looks like this: { "a": 123, "b": 0.26, "c": [HUGE irrelevant object], "d": 32 } I only want the integer values stored for keys a , b and d and ignore the rest of the JSON (i.e. ignore whatever is there in the c value). I cannot modify the original JSON as it is created by a 3rd party service, which I download from its server. How do I do this without loading the entire file in memory? I tried using gson library and created the

Unable to parse Json array using Gson

╄→尐↘猪︶ㄣ 提交于 2019-12-28 18:04:27
问题 I have this json array: [ { "id":18, "city":"הרצליה", "street":"החושלים 1", "zipcode":121209, "state":"IL", "lat":32.158138, "lng":34.807838 }, { "id":19, "city":"הרצליה", "street":"אבא אבן 1", "zipcode":76812, "state":"IL", "lat":32.161041, "lng":34.810410 } ] And i have this class to hold the data: public class MapData { private int id; private String city; private String street; private String state; private int zipcode; private double lat; private double lng; public MapData(int id, String

Converting JSON between string and byte[] with GSON

梦想与她 提交于 2019-12-28 17:41:42
问题 I am using hibernate to map objects to the database. A client (an iOS app) sends me particular objects in JSON format which I convert to their true representation using the following utility method /** * Convert any json string to a relevant object type * @param jsonString the string to convert * @param classType the class to convert it too * @return the Object created */ public static <T> T getObjectFromJSONString(String jsonString, Class<T> classType) { if(stringEmptyOrNull(jsonString) ||

How to dynamically handle json response array/object using Gson

橙三吉。 提交于 2019-12-28 12:30:09
问题 I am facing a problem , sometimes Json response returns an array of objects , sometimes object itself , how we can handle dynamically in the response class. In the current eg :results sometimes gets an array of objects "\"results\": " + "[{" + and sometimes object itself "\"results\": " + "{" + Eg: How we can handle this ? Gson gson = new Gson(); SearchResponse response=new SearchResponse(); response= gson.fromJson("{" + "\"completed_in\": 0.047," + "\"max_id\": 291771567376039936," + "\"max

How to dynamically handle json response array/object using Gson

一曲冷凌霜 提交于 2019-12-28 12:30:00
问题 I am facing a problem , sometimes Json response returns an array of objects , sometimes object itself , how we can handle dynamically in the response class. In the current eg :results sometimes gets an array of objects "\"results\": " + "[{" + and sometimes object itself "\"results\": " + "{" + Eg: How we can handle this ? Gson gson = new Gson(); SearchResponse response=new SearchResponse(); response= gson.fromJson("{" + "\"completed_in\": 0.047," + "\"max_id\": 291771567376039936," + "\"max

How do I convert a JSONObject to class object?

做~自己de王妃 提交于 2019-12-28 05:59:10
问题 I need to convert a JSONObject to a Location object using gson libraries for an android project of mine. I'm not sure on how to do this. Can anyone help me with this. Thanks in advance. I have a code something like JSONArray input = new JSONArray(extras.getString("loc_json")); I wanted to convert the JSONObject taken from the JSONArray to a Location class object. I just wanted to know whether there is a function that does it directly. Pardon me if I framed the question in a wrong way. Since I

Using generics with GSON

百般思念 提交于 2019-12-28 04:01:11
问题 I am using GSON to decode JSON into an object of type T e.g. public T decode(String json) { Gson gson = new Gson(); return gson.fromJson(json, new TypeToken<T>() {}.getType()); } This however returns an exception - java.lang.AssertionError: Unexpected type. Expected one of: java.lang.reflect.ParameterizedType, java.lang.reflect.GenericArrayType, but got: sun.reflect.generics.reflectiveObjects.TypeVariableImpl, for type token: T I thought that by using TypeToken I avoided Type Erasure. Am I

GSON parsing without a lot of classes

天大地大妈咪最大 提交于 2019-12-28 03:02:59
问题 I have the following JSON and I'm only interested in getting the elements "status" , "lat" and "lng" . Using Gson , is it possible to parse this JSON to get those values without creating the whole classes structure representing the JSON content? JSON: { "result": { "geometry": { "location": { "lat": 45.80355369999999, "lng": 15.9363229 } } }, "status": "OK" } 回答1: You don't need to define any new classes, you can simply use the JSON objects that come with the Gson library. Heres a simple

Deserializing Generic Types with GSON

走远了吗. 提交于 2019-12-27 17:04:15
问题 I have some problems with implementation of Json Deserialization in my Android application (with Gson library) I've made class like this public class MyJson<T>{ public List<T> posts; } And Deserialization call is: public class JsonDownloader<T> extends AsyncTask<Void, Void, MyJson<T>> { ... protected MyJson<T> doInBackground(Void... params) { ... Reader reader = new InputStreamReader(content); GsonBuilder gson = new GsonBuilder(); Type collectionType = new TypeToken<MyJson<T>>() {}.getType();

Deserializing Generic Types with GSON

北战南征 提交于 2019-12-27 17:03:24
问题 I have some problems with implementation of Json Deserialization in my Android application (with Gson library) I've made class like this public class MyJson<T>{ public List<T> posts; } And Deserialization call is: public class JsonDownloader<T> extends AsyncTask<Void, Void, MyJson<T>> { ... protected MyJson<T> doInBackground(Void... params) { ... Reader reader = new InputStreamReader(content); GsonBuilder gson = new GsonBuilder(); Type collectionType = new TypeToken<MyJson<T>>() {}.getType();