Map和javabean的轉換

2021-08-28 10:15:09 字數 1322 閱讀 9607

public static void main(string args)

}// 物件即使是擁有相同鍵的map物件也不能複製屬性給map物件

map map3 = new linkedhashmap<>();

map3.put("code",0);

map3.put("data", null);

map3.put("msg", "");

beanutils.copyproperties(map3, map);

system.out.println("**********"+map3);

beanutils.copyproperties(map3, result);

system.out.println("************"+map3);

//將map的值依次賦給相應的屬性

map.put("code", 3);

map.put("data", "test");

map.put("msg", "lala");

for (propertydescriptor propertydescriptor : properties)

object value = map.get(propertyname);

beanutils utils = new beanutils();

beanutils.copyproperty(result, propertyname, value);

}system.out.println("***********************************");

system.out.println(result);

system.out.println("***********************************");

// 擁有相同鍵的map物件可以直接賦值給物件的相應屬性

map map2 = new linkedhashmap<>();

map2.put("code", 4);

map2.put("data", "tttttt");

map2.put("msg", "hahhahaahah");

map2.put("nae", "jdflkdj");

beanutils.copyproperties(result, map2);

system.out.println(result.tostring());

} catch (introspectionexception | illegalacces***ception | illegalargumentexception | invocationtargetexception e)

能夠轉換中文亂碼的JavaBean

public class charactorencoding public charactorencoding public string tostring string str string text if str null equals str try text new string str.g...

json和map的轉換

map轉換成json mapmap new hashmap map.put a aaa map.put b bbb map.put c ccc string json json.tojsonstring map system.out.println json 輸出 注意 tojsonstring這個...

json和map的轉換bug

map轉換成json mapmap new hashmap map.put a aaa map.put b bbb map.put c ccc string json json.tojsonstring map system.out.println json 輸出 注意 tojsonstring這個...