在閱讀形式爲Map<String, Object>.
的地圖時遇到問題此對象可以是String/PrimitiveWrapper/List/Map。如果對象是一個Map,那麼這個Map可以包含一個List,並且這個List可以包含一個Map,.....等等。這是第n級。我需要迭代Object並獲取值。我嘗試了不同的方式,但沒有成功。發生看到下面的帖子,但不能得到所需的輸出。如何閱讀複雜的地圖和集合
Recursively reading any java Object and pulling out complex types into a hash map
誰能請幫助我獲得所需的輸出。我附上了一個示例程序,並且還顯示了示例輸出。
關於第一輸出的簡單介紹:
客戶是地圖八個關鍵,客戶是地圖七個關鍵。地圖七包含一個數組六,所以鍵應顯示爲Customer [index]。六[0]是地圖5A,其關鍵是地址。 FiveA包含一個Map-Four,其關鍵是地址。四個包含一個列表 - 三個,所以四個鍵應顯示爲地址[index]。三個[0]是一個帶有密鑰的Map-One,地址爲AddressLine1,其值爲House 1。 所以最終鍵和值是:
Customers.Customer [0] .Addresses.Address [0] = .AddressLine1 House在提前1個
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class StructureReader {
public static void main(String[] args) {
StructureReader reader = new StructureReader();
Map<String, Object> firstData = reader.getFirstData();
// After iterating the first data
// Output should be
// key = value
// Customers.Customer[0].Addresses.Address[0].AddressLine1 = House 1
// Customers.Customer[0].Addresses.Address[1].AddressLine2 = Street 1
// Customers.Customer[1].Name.FirstName = Joe
// Customers.Customer[1].Name.Surname = Bloggs
Map<String, Object> secondData = reader.getSecondData();
// After iterating the second data
// Output should be
// l2Map.l3List[0] = some_string
// l2Map.l3List[1] = 123
// l2Map.l3List[2] = 100.5
// l2Map.l3List[3] =
// l2Map.l3List[4] =
}
private Map getFirstData() {
Map one = new HashMap();
one.put("AddressLine1", "House 1");
Map two = new HashMap();
two.put("AddressLine2", "Street 1");
List three = new ArrayList();
three.add(one); // index 0
three.add(two); // index 1
Map four = new HashMap();
four.put("Address", three);
Map fiveA = new HashMap();
fiveA.put("Addresses", four);
Map fiveB = new HashMap();
fiveB.put("FirstName", "Joe");
fiveB.put("Surname", "Bloggs");
Map fivec = new HashMap();
fivec.put("Name", fiveB);
List six = new ArrayList();
six.add(fiveA); // index 0
six.add(fivec); // index 1
Map seven = new HashMap();
seven.put("Customer", six);
// Final HashMap i.e HashMap<String, Object>
// Now the Object is seven
Map eight = new HashMap();
eight.put("Customers", seven);
return eight;
}
private Map<String, Object> getSecondData() {
Map<String, Object> inputMap = new HashMap<String, Object>();
Map<String, List<Object>> level2Map = new HashMap<String, List<Object>>();
List<Object> level3List = new ArrayList<Object>();
String level4String = "some_string";
Integer level4Integer = 123;
Double level4Double = 100.5d;
List<Object> level4List = new ArrayList<Object>();
Map<String, Object> level4Map = new HashMap<String, Object>();
level3List.add(level4String);
level3List.add(level4Integer);
level3List.add(level4Double);
level3List.add(level4List);
level3List.add(level4Map);
level2Map.put("l3List", level3List);
inputMap.put("l2Map", level2Map);
return inputMap;
}
}
感謝。
可汗
你的設計有一個難聞的氣味。因此修復設計而不是向錯誤代碼添加更多代碼。 – 2011-03-09 05:43:59
我同意你的看法,但這是我獲取數據到組件的方式。現在我正在通過遞歸程序來生成輸出。部分能夠獲得輸出。請讓我知道我可以在哪裏附上我的代碼以供其他人查看。 – user650900 2011-03-09 09:49:09
我們可以通過使用二叉樹橫向來讀取上述結構嗎? – user650900 2011-03-09 11:04:51