我有一个json数组列表,格式相同,格式相同。我不能指望排序中的数据,它可能会有所不同,Company-x可能会以任何顺序出现。
Company-1 - {Branch-1, Branch-4, Branch-8}
Company-2 - {Branch-9, Branch-11, Branch-22}
Company-4 - {Branch-45, Branch-64, Branch-18}
Company-1 - {Branch-10, Branch-43, Branch-82}
Company-2 - {Branch-15, Branch-44, Branch-86}
此外,这可能会增加超过10,000行。我需要在不影响性能的情况下创建一个键值对。我需要在HashMap中存储以上数据。 (Map< String,List< Branch>)。
例如,我需要以下格式的结果:
[Company-1,{Branch-1, Branch-4, Branch-8,Branch-10, Branch-43, Branch-82}]
[Company-2,{Branch-9, Branch-11, Branch-22,Branch-15, Branch-44, Branch-86}]
[Company-4,{Branch-45, Branch-64, Branch-18}]
....etc.
如何在java中实现这一点?
这是json格式。键是CID,值是(Branch_ID和名称)
[
{
"CID": "4545",
"BRANCH_ID": "0041",
"Name": "BID41",
},
{
"ID": "4546",
"BRANCH_ID": "0051",
"Name": "BID51",
},
{
"ID": "4545",
"BRANCH_ID": "0042",
"Name": "BID42",
},
{
"ID": "4546",
"BRANCH_ID": "0052",
"Name": "BID52",
},
{
"ID": "4545",
"BRANCH_ID": "0043",
"Name": "BID43",
},
{
"ID": "4545",
"BRANCH_ID": "0053",
"Name": "BID53",
}
]
答案 0 :(得分:0)
如果您尝试将Json数据转换为Map<字符串,列表> ,这是解决方案:
public Map<String , List<String>> convertMapList(String jsonStr) {
Type mapType = new TypeToken<Map<String, Map>>(){}.getType();
Map<String, List<String>> son = new Gson().fromJson(jsonStr, mapType);
return son;
}
为此,您必须在项目中添加Gson库