Home > Back-end >  About the HashMap. ToString JSON related issues
About the HashMap. ToString JSON related issues

Time:09-18

If a project has a lot of the Map and List, then log in for a large number of Map and List related information
However, when there is a strange question, we must restore the scene, then need to log in the Map and a List of String to the Object of transformation

At present more popular Jackson (FasterXML) is aimed at JOSN into Java objects, you need to Map the toString, List. ToString into standard JOSN,

I didn't find a better solution
Temporarily by the regular expression, processing,

Informed CSDN the great god, help me staff staff, now have a more off-the-shelf tools can solve the above problem? Or is there a method to encourage myself to write grafting bridge? A JOSNMap or decisive rewrite HashMap, do?

CodePudding user response:

Oneself write a Util class, by iterating through the Map, List, into a Json String, positive direction and reverse direction, respectively, to write a method, also is not difficult, why to find?

CodePudding user response:

With JSONObject and JSONArray, the scene is very suitable for you

CodePudding user response:

The original poster can try to use the Json - Simple class libraries, the libraries of the JSONObject can directly join the Map and JSONArray directly to join the List, and can be through the toString Json instance into or through a Json string Parser will string into Json example,
If you want to achieve, you need to traverse your Map, List, and then a string, a formatted output you actually if the Map or List of fixed format, as long as it is formatted file can be, do not necessarily need to JSON, just look at convenient,

CodePudding user response:

I think, the original poster should be in order to output in the Logger HashMap Json representation instead of the traditional toString form, can in order to "restore the scene",
The exact explanation is not to say, write to the original poster example:
//JsonMap 
Public static class JsonMap Extends HashMap {

Private static final long serialVersionUID=- 4083915643878380063 l;

//from the Json structure Map
Public static & lt; K, V> JsonMap FromJson (String json) {
@ SuppressWarnings (" unchecked ")
JsonMap The map=JSON. ParseObject (JSON, JsonMap. Class);
return map;
}

//rewrite the toString method
@ Override
Public String toString () {
Return a JSON. ToJSONString (this);
}
}

//test
Public static void main (String [] args) {
JsonMap Jmap=new JsonMap (a);
Jmap. Put (" hello ", "world");
Jmap. Put (" hello ", "world");
String jmapString=jmap. ToString ();
System. The out. Println (jmap);//here will automatically call the toString ()
Jmap=JsonMap. FromJson (jmapString);
System. The out. Println (jmap. Get (" hello "));
}

In this way, only need to change the program need to recover the scene in the HashMap to JsonMap, then modify the recovery logic,
Of course, you can also will be packed in fromJson toString method into a utility class, this requires you to choose the specific business environment,

CodePudding user response:

Use regular expressions, map object into a json string format is ok after the toString
 
The Map test=new HashMap ();
Test. The put (" a ", "1");
Test. The put (" b ", "2");
Test. The put (" c ", "3");
String testStr=test. The toString ();
System. The out. Println (testStr);

String jsonStr=testStr. ReplaceAll (" ([^, \ \ {\ \ s] *)=(\ \}] [^, \ \] *) ", "\ " $1 ". \ "" $2 ");
System. The out. Println (jsonStr);

System. The out. Println (new ObjectMapper () readValue (jsonStr, HashMap. Class));
  • Related