I'm using Gson in an Android app to convert a complicated object to JSON representation. One of the fields is a string called QuickPin containing an encrypted password and the character "=" is converted to "\003d" by Gson.
The Json string is consumed by a C# WEBAPI application, but returns "an error has occurred" message.
The following JSON returns that error message :
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":"MethodCombo":{"AuthMethod":[1]},"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ\u003d\u003d"},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
Meanwhile this JSON works fine :
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":{"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ==","MethodCombo":{"AuthMethod":[1]}},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
Are there a way to force Gson to maintain the string with the password with the = (and others if is the case) characters?
My Android code is :
Gson gson = new Gson();
user = new User();
user.UserAuthentication = new UserAuthentication();
user.UserAuthentication.QuickPin = "mW2n2uTECEtVqWA2B9MzvQ==";
user.UserAuthentication.MethodCombo = new MethodCombo();
user.UserAuthentication.MethodCombo.AuthMethod = new ArrayList<Integer>();
user.UserAuthentication.MethodCombo.AuthMethod.add(1);
user.Status = 0;
String jsonRepresentation = gson.toJson(user);
object.put("user", jsonRepresentation);
Thanks
Gson escapes HTML metacharacters by default. You can disable this behavior.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
Related
I'd like to understand the difference with a server returning value verses that value being completely absent in the response.
lets take at what i have so far:
data class MyApiResponse(#SerializedName("name") val name: String,
#SerializedName("address") val address: String,
#SerializedName("max_time") val maxTime: Double? = null //this field might BE COMPLETELY absent in response, what will happen here ?
)
regarding the maxTime value, if the value is COMPLETELY absent from server response, will the app crash or will the value be null ?
the issue is im trying to distinguish between server sending
max_time: null vs it being completely absent ,how does gson handle this ?
In both cases it will be null.
Might look here for more info:
Gson optional and required fields
Please try this solution:
String json = ""; //Your json has a String
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
String name = jsonObject.get("name").toString();
String adress = jsonObject.get("address").toString();
//If null, use a default value
JsonElement max_time = jsonObject.get("max_time");
String text = (max_time instanceof JsonNull) ? "" : max_time.getAsString();
String json = ""; //Your json has a String
Gson gson = new GsonBuilder().serializeNulls().create();
MyApiResponse myApiResponse = gson.fromJson(json, YOURCLASSNAME.class);
my question is about the following.
Im just trying to generate a new txt file in my internal storage. Inside it i will have just an array converted to string to save some IDs that i need to persist.
After that i need to read that file too but i don't know how to start.
I think is something like this:
private void GsonWriter(ArrayList<Integer> arrayListTouched){
String str = arrayListTouched.toString();
Gson gson = new GsonBuilder().create();
Since you're already using Gson.
How to save it.
Gson gson = new GsonBuilder().create();
ArrayList<Integer> array = new ArrayList<>(); // Integer in your case. But can be any type
// save this string
String arrayString = gson.toJson(array);
How to load it.
ArrayList<Integer> array = gson.fromJson(arrayString, new TypeToken<ArrayList<Integer>>(){}.getType());
I assume you know how to save/load the string
I want to remove attributes that I specify collections using Gson.
My code:
SharedPreferences sharedPreferences = parentActivity.getSharedPreferences(Accommodation.UPLOAD_ACCOMMODATION_DETAILS_ALL, MODE_PRIVATE);
// Get saved string data in it.
String userInfoListJsonString = sharedPreferences.getString(Accommodation.UPLOAD_ACCOMMODATION_DETAILS, "");
// Create Gson object and translate the json string to related java object array.
Gson gson = new Gson();
Accommodation userInfoDtoArray = gson.fromJson(userInfoListJsonString, Accommodation.class);
// Loop the UserInfoDTO array and print each UserInfoDTO data in android monitor as debug log.
// Get each user info in dto.
Accommodation userInfoDto = userInfoDtoArray;
I printed Json and it have this:
{"accommodationName1":"Green Flat",
"accommodationName2":"Bangolor",
"accommodationName3":"GHost house",
"imgAccommodation1":"/data/user/0/com.xxx.xxx/cache/cropped6900224487159235524.jpg",
"imgAccommodation2":"/data/user/0/com.xxx.xxx/cache/cropped3411178797945328810.jpg",
"imgAccommodation3":"/data/user/0/com.xxx.xxx/cache/cropped9128365945226539316.jpg"}
I can use this code to get the value:
userInfoDto.accommodationName1;
It show the value :
Green Flat
So how I can remove the specific collection of that value?
I am using realm to store and retrieve utf-8 for nepali(Devnagari) characters but when retrieving � is also generating which is not a nepali(Devnagari) character and not stored. How to solve this issue? Along with realm I have used Gson and retrofit.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
final RealmResults<Information> realmObj = realm.where(Information.class).findAll();
if (realmObj != null) {
List<Information> obj = realm.copyFromRealm(realmObj);
String json = gson.toJson(obj);
}
which generates
२५ द���खि ४० वर्षः
How to Convert this
[{destLocId=10, createdUserId=b9ab2d71-9a69-4ba3-b498-d36446a154d6,
createdDate=2016-6-29 14:35:00}]
to this :
[{"destLocId":10, "createdUserId":b9ab2d71-9a69-4ba3-b498-d36446a154d6,
"createdDate":2016-6-29 14:35:00}]
You can use GSON library for converting ArrayList to JSON. Here an example,
ArrayList<String> bibo = new ArrayList<String>();
bibo.add("obj1");
bibo.add("obj2");
bibo.add("obj3");
String json = new Gson().toJson(bibo);
And this example from Gson User Guide to use it on collection.