2016-05-16 29 views
0

我嘗試使用Apache下議院豆的Util上通用的地圖,像這樣的通用陣列上的setProperty:的BeanUtils - 地圖

這是地圖:

public class MyObject { 
    public Map<String, ?> attributes = new HashMap<String, Object>(); 
} 

這裏是FO JSON表示什麼在地圖:

"attributes": { 
     "attr1": "value1", 
     "attr2": "value2", 
     "attrN": "valueN", 
     "nestedObject" :{ 
      "nestedAttr" : "value1", 

      "nestedAttr2" : "value2" 
     }, 
     "simpleArray":["value1", "value1"] 
    } 

這裏是我想使用的setProperty來修改值之一:

org.apache.commons.beanutils.BeanUtils.setProperty(myObject, "attributes.simpleArray[0]", "newValue"); 

嵌套屬性正常工作。然而,當嵌套屬性之一是一個數組我出現以下情況例外:

java.lang.IllegalArgumentException異常:索引或映射屬性不支持類型的地圖對象:simpleArray [0]

此工作,如果我嘗試設置一個簡單的屬性,如:

org.apache.commons.beanutils.BeanUtils.setProperty(myObject, "attributes.attr1", "newValue"); 

但不是與數組。這裏有什麼問題?

回答

0

正如錯誤所述,您可以將新數組設置爲數組字段,但不是數組的特定索引。 Bean utils在字段getter/setter上工作,因此它可以獲取/設置對象的字段,但不能獲取數組或列表或映射的特定索引。