Home > Back-end >  Questions about generic conversion problem
Questions about generic conversion problem

Time:12-10

I have such a method:



Mean value obtained through key, and will he converted to the corresponding type, now I have a data in redis, format for:



Here's the thing, I will he converted to one of my type, called AdminDetails, this class are defined as follows:



Run the code, some problems appear, look at the picture:



I use Object accept Object, because I am that a string of JSON format, he gave me type automatically converted to the LinkedHashMap, eventually leading to failure I conversion, but I actually incoming type AdminDetails

This type of format, how to correct the transformation, beg of bigwigs doubt!!!!!!!!!!!!!!!!!!!!
But beyond that, the get method can also operate a lot of other types, such as String, Map, etc.

By the way, I set the cache code is as follows:

CodePudding user response:

Using json utility class
The import net. Sf. Json. JSONArray;
The import net. Sf. Json. JSONObject;

CodePudding user response:

JsonObject cannot parse, he automatically transformed into LinkedHashMap, but I want to specify AdminDetails, cannot successfully parsed

CodePudding user response:

Redis values is a deserialization process, he didn't know you are in the process is put into a bean type, endures redis is a serialization process, when I see you use the objectmapper, so the simplest you directly use the string to accept you to get the value, then objectmapper. ReadValue (STR, AdminDetail. Class) to fetch it, he will automatically help you deserialized into your AdminDetail object

CodePudding user response:

My way is directly to the result of strong to T, and then call the get method with AdminDetails directly accept there, but at least need to accept to use JSON. ToJSONString into String, again using JSON. ParseObject into AdminDetails, like this are still confused, I clearly deposit is AdminDetails the JSON data object, why not take out using direct assignment, for example: AdminDetails aaa=redisTemplate. Get (XXX) like this,

CodePudding user response:

references from entry to the 4th floor begging response:
my practice is directly to the results to a T, and then call the get method with AdminDetails directly accept there, but at least need to accept to use JSON. ToJSONString into a String, and then using JSON. ParseObject into AdminDetails, it still confused, I clearly deposit is AdminDetails the JSON data object, why not take out using direct assignment, for example: AdminDetails aaa=redisTemplate. Get (XXX) this way,

Mobile phone, a little wrong character: before receiving need to use...

CodePudding user response:

reference im_thirteen reply: 3/f
redis values is a deserialization process, the whole process is he didn't know you deposit is a type of bean, endures redis is a serialization process, I see you with objectmapper, so the simplest you directly use the string to accept you to get the value, then objectmapper. ReadValue (STR, AdminDetail. Class) to get, he will automatically help you deserialized into AdminDetail object you want

Ok, I try tomorrow

CodePudding user response:

See website document, JSon is read out a Map, need to use HashMapper conversion,
 

Public class Person {
String firstname.
String lastname.

//...
}

Public class HashMapping {

The @autowired
HashOperations
HashMapper
Public void writeHash (String key, the Person the Person) {

MapHashOperations. PutAll (key, mappedHash);
}

Public Person loadHash (String key) {

MapReturn (Person) mapper. FromHash (loadedHash);
}
}
  • Related