I need to find a way of saving a HashMap to string to save in my SQLite database to retrieve on a later stage.
Here is my HashMap:
private Map<String, Bitmap> myMarkersHash new HashMap<String, Bitmap>();
Then when saving to the SQlite database:
contentValues.put(LocationsDB.FIELD_HASH, myMarkersHash);
But the problem is i get an error under put at contentValues.put with this error:
The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Map<String,Bitmap>)
So my question is how can i convert that to a string so that i can save it into my sqlite database?
Thanks
You can store your HashMap in your Database by converting it into String using Gson library.
This library will convert your object to String and you will get back that object whenever you want it.
Link to Download Lib : https://code.google.com/p/google-gson/
Convert Object into String :
Gson objGson= new Gson();
String strObject = objGson.toJson(myMarkersHash);
Bundle bundle = new Bundle();
bundle.putString("key", strObject);
Convert String into Object :
Gson gson = new Gson();
Type type = new TypeToken<Model>() {}.getType();
myMarkersHash = gson.fromJson(bundle.getString("key"), type);
Check this example as well : http://www.java2blog.com/2013/11/gson-example-read-and-write-json.html
Related
I'm new to using JSONs and I'm having a bit of difficulty. I am using the Kitsu API and parsing the JSON I get when I login. When I parse my json the image below pops up, but inside the 1 object array I want to get the large url in the avatar object inside of the attributes object and I don't know how.The beginning of the JSON, The middle of the JSON, The end. Lastly, I want to know how to edit the slug part of the JSON, if you don't know that's cool the main thing is getting the avatar url.
Picture of my error
Please, try this...
JSONObject myObject = new JSONObject(jsonStr);
JSONArray myArray = myObject.getJSONArray("data");
JSONObject obj = myArray.getJSONObject(0);
String id = obj.getString("id");
String type = obj.getString("type");
//change the lines bellow
JSONObject attributes = obj.getJSONObject("attributes");
JSONObject avatar = attributes.getJSONObject("avatar");
String large = avatar.getString("large");
I have a JobDataMap Object
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String[] key = dataMap.getKeys();
key is foo as String,
key is data data have a json like this=> {"abc":"xyz","pqr":"123wer"}.
I want to get values from data object and set to String.
For eg: String abc = data.abc; Here I want to set value from data.
How do I get values from data object? Please help me....
solution is below:
String dataValue = dataMap.get("data").toString();
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(dataValue);
or we can also use objectMapper instead JSONParser.
String abc = (String) json.get("abc")
I have a json stored in a DB like this,
"supported_iso_codes":[
{
"EUR": "978",
"USD": "840"
}
],
To access this in my app code, I do something like this..
getISOProfileDB.getSupportedISOCodes();
I have a string which the user inputs(provides input string like EUR, USD,etc). How can I convert the above json to a HashMap and compare it with another string? What I am trying to achieve is,
Compare Key part of json to user input string(EUR).
If both of them match,
Parse the value part of json and store it in a variable.
Below is what I'm trying to achieve,
tran.setCurrency(hashMapOfJson.get(currencyString));
Use Gson :
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
And then :
Map<String, Object> supported_iso_codes = new Gson().fromJson(getISOProfileDB.getSupportedISOCodes(), new TypeToken<HashMap<String, Object>>() {}.getType());
You can do something like this
Gson gson = new Gson();
Type type = new TypeToken<List<Map<String, String>>>(){}.getType();
final ArrayList<HashMap<String,String>> isoCodesMapList = gson.fromJson(data, type);
System.out.println(arrayList);
then for getting the user selected currency you can do
isoCodesMapList.get(userSelectedCurrency);
Hope this helps:)
You should consider using JSONObject (Jsonobject.org) or Gson.
I would like to know if it is possible to convert any Java object to JSON object. Currently I have the following code.
JSONArray data = new JSONArray();
for (User user : users) {
JSONArray row = new JSONArray();
row.put(user.getId()).put(user.getUserName()).put(user.isEnabled());
data.put(row);
}
The current issue is different object (e.g. User and Admin) will have different property, thus the above code will work for other object. I am thinking of putting a similar code in my GenericHibernateDAO in order to automatically convert any list into a json list.
You can serialize your java object to json object. There are n number of library is available ex gson, jettyson, flexjson etc.
GSON example -
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
Here i exemplify the way of converting POJO to json using jackson
create your pojo : User user = new User();
you can set or get values to/from user
create ObjectMapper : ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);// object to json
I need to convert a Vector<Vector<Float>> to a JSONArray. Apart from iterating through the vector and creating the JSONArray, is there any simpler way to do this?
Someone told me to try gson.
SharedPreferences is just a key-value store. What's stopping you from bypassing JSONObject completely, and just using something like this (Gson only)?
private static final Type DATA_TYPE =
new TypeToken<Vector<Vector<Float>>>() {}.getType();
Storage:
Vector<Vector<Float>> data = new Vector<Vector<Float>>();
data.add(new Vector<Float>());
data.get(0).add(3.0f);
String dataAsJson = new Gson().toJson(data, DATA_TYPE);
sharedPreferences.edit().putString("data", dataAsJson).commit();
Retrieval:
String dataAsJson = sharedPreferences.getString("data", "[]");
Vector<Vector<Float>> data = new Gson().fromJson(dataAsJson, DATA_TYPE);
Disclaimer: I've never developed for Android.