Home > Back-end >  Consult! Complex JSON and MAP transformation problem
Consult! Complex JSON and MAP transformation problem

Time:09-29

I wrote a JSON conversion tool, can convert complex JSON to MAP,
But I find that I can't revert to its JSON, strives for the bosses help under should think about how to do it.
Before the transformation:
 
{
"MSG" : "OK,"
"Code" : 0,
"Data" : {
"The product" : [
{
"Name" : "ordinary products,"
"Id" : 1
},
{
"Name" : "membership product,"
"Id" : 2
}
],
"Sort" : [
2,
1
]
}
}

After the transformation:
 
{
"MSG" : "OK,"
"The data. The product [0]. Id" : 1,
"The data. The product [1]. The id" : 2,
"The data. The product [0]. The name" : "ordinary products,"
"Code" : 0,
"The data. The product [1]. The name" : "membership product,"
"Data. Sort [0]" : 2,
"Data. Sort [1]" : 1
}

The source code is as follows:
 
Package utils;

The import com. Alibaba. Fastjson. JSON;
The import com. Alibaba. Fastjson. JSONObject;

Import the Java. Util. *;

/* *
* @ author adinlead
*/
Public class JsonMapper {
Public static Map SpreadJsonObject (JSONObject obj) {
Map Data=https://bbs.csdn.net/topics/new HashMap <> (obj. The size ());
SpreadMap (null, data, obj);
return data;
}

Private static void spreadMap (String baseKey Map Target, the Map Map) {
If (baseKey==null) {
BaseKey="";
} else {
BaseKey +=". ";
}
For (the Object key: map. KeySet ()) {
Object val=map. Get (key);
If (val instanceof Map) {
SpreadMap (baseKey + key, target, val (Map));
{} else if (val instanceof List)
SpreadList (baseKey + key, target, val (List));
} else if (val instanceof Object []) {
SpreadList (baseKey + key, target, the Arrays. The asList ((Object []) val));
} else {
Target. The put (baseKey + key, val);
}
}
}

Private static void spreadList (String baseKey Map Target, the List array) {
If (baseKey==null) {
BaseKey="";
}
for (int i=0; I & lt; Array. The size (); I++) {
The Object val=array. Get (I);
If (val instanceof Map) {
SpreadMap (baseKey + "/" + I + ", "target, val (Map));
{} else if (val instanceof List)
SpreadList (baseKey + "/" + I + ", "target, val (List));
} else if (val instanceof Object []) {
SpreadList (baseKey + "/" + I + ", "target, Arrays. The asList ((Object []) val));
} else {
Target. The put (baseKey + "/" + I + ", "val);
}
}
}

Public static void main (String [] args) {
String test="{" code \ ": 0, " MSG \ ": " OK \ ", \ "data " : {\ product \ "" : [{\ " id ": 1, " name \ ": \ " common product "}, {\ \ "id" : 2, \ "name " : \ \ "member products"}], \ "sort " : [2, 1]}} ";
JSONObject testObject=JSONObject. ParseObject (test);
Map Result=spreadJsonObject (testObject);
System. The out. Println (JSON. ToJSONString (testObject, true));
System. The out. Println (JSON. ToJSONString (result, true));
}
}

CodePudding user response:

I don't think it's necessary so write
Public static HashMap JsonObjectToHashMap (JSONObject jsonObj) {
HashMap Data=https://bbs.csdn.net/topics/new HashMap ();
The Iterator it=jsonObj. Keys ();
While (it. HasNext ()) {
String key=String. The valueOf (it. Next (), toString ());
The String value=https://bbs.csdn.net/topics/(String) jsonObj. Get (key). The toString ();
Data. The put (key, value);
}
System. The out. Println (data);
return data;
}

CodePudding user response:

Not line, a fierce like a tiger operation

Public static void main (String [] args) {
String test="{" code \ ": 0, " MSG \ ": " OK \ ", \ "data " : {\ product \ "" : [{\ " id ": 1, " name \ ": \ " common product "}, {\ \ "id" : 2, \ "name " : \ \ "member products"}], \ "sort " : [2, 1]}} ";
JSONObject testObject=JSONObject. ParseObject (test);
Map m=JSONObject parseObject (testObject toJSONString (), Map. The class).
String STR=JSON. ToJSONString (m);
System.out.println(str);

}

CodePudding user response:

Too complex structures, such as their definition of bean, the class can be passed as a parameter to go in, need not so difficult to write a bunch of parsing, elegant code
  • Related