简体   繁体   English

Java序列化和反序列化

[英]Java serialization and deserialization

Please Help me to convert this请帮我转换这个

{"Map1":"{\"t1x\":\"Test1x\",\"t2x\":\"Test2x\",\"t3x\":\"Test3x\"}","Map":"{\"t1\":\"Test1\",\"t2\":\"Test2\",\"t3\":\"Test3\"}"} 

to

{
"Map1":{
"t1":"Test1x",
"t2":"Test2",
"t3":"Test3"
},
"Map":{
"t1":"Test1",
"t2":"Test2",
"t3":"Test3
    }
}

Please find below my class请在我的班级下方找到

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.webinbox.shared.CommonFunctionality;

public class Test implements Serializable{

public static void main(String[] args) {
    String json = ret();

    System.out.println(json); 
}

    public static String ret() { 
        LinkedHashMap<String,String>map=new LinkedHashMap<String,String>();
        map.put("t1", "Test1");
        map.put("t2", "Test2");
        map.put("t3", "Test3");

        LinkedHashMap<String,String>map1=new LinkedHashMap<String,String>();
        map1.put("t1x", "Test1x");
        map1.put("t2x", "Test2x");
        map1.put("t3x", "Test3x");

        Map<String,LinkedHashMap<String, String>>linkedMap=new HashMap<String,LinkedHashMap<String, String>>();

        linkedMap.put("Map",map);
        linkedMap.put("Map1", map1);
        CommonFunctionality commonFunctionality = new CommonFunctionality();
        JsonObject jsObj=new JsonObject();
        jsObj=commonFunctionality.mapToNestedJson(linkedMap);
        String jsonString=jsObj.toString();
        return jsonString; 
    }

}
public JsonObject mapToNestedJson(Map<String,LinkedHashMap<String, String>>inpMap) {
JsonObject jsRes = new JsonObject();
JsonObject jsResChild = new JsonObject();
    Set<String> keys = inpMap.keySet();
    LinkedHashMap<String, String> values = null;
    Set<String> mapKeys = null;
    for(String key: keys){
        values=inpMap.get(key);
         mapKeys=values.keySet();
         jsResChild=new JsonObject();
        for(String keyValues:mapKeys)
        {jsResChild.addProperty(keyValues,values.get(keyValues));
    }
        jsRes.addProperty(key, jsResChild.toString());
    }

return jsRes;

} } This is my mapToNestedJson method --- @Robby Cornelissen这是我的 mapToNestedJson 方法 --- @Robby Cornelissen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM