![](/img/trans.png)
[英]How to construct Map<String, List<String>> data structure in Spring
[英]How to construct a complex json structure using Map<String, String> in java
我正在解決這個問題中發現的類似403錯誤
摘要是,我正在做一個簡單的帶有JSON數據的http POST,並將其作為http正文。 而不是200的響應,我得到403,然后再深入探討為什么我認為我會在所引用的問題中聽取用戶的建議,並使用此Map結構手動構造json字符串。 唯一的問題是我不確定如何對類似以下的復雜結構執行此操作(例如,地圖應包含地圖)
{"context":{"locationdata":{"lat":41.5816456,"lng":-93.62431329999998}},"results":{"less":150,"on":true,"off":true,"status":true,"working":true,"item":[1111]}}
先感謝您
創建了自己的json生成工具后,在一個我從事的項目中,所做的事情非常相似。 映射表示對象文字,而列表表示數組。 因此,我們有一些地圖,其中包含有地圖的列表。 我們的實用程序將遞歸檢查每個屬性的類型,是否是列表調用一個方法,是否是映射調用另一個方法。 我們的工具有類似
public String writeJson(Map map, String json) {
/*
Code that looped thru the entries of the map and determined whether to
1. add a property to the String for a simple type
2. recurse into this method if the entry contained a Map
3. call writeJson(list) if the entry contained a List
*/
}
public String writeJson(List list, String json) {
// same comment as above
}
如果您想自己滾動,即使對於深度嵌套的結構,也可以嘗試做。 我們的工具大約有100行代碼。 但是,現在有好的第三方庫可以做到這一點。
請注意,在您的問題標題中提到了Map<String, String>
。 您必須將其更改為Map<String, ?>
或Map<String, Collection>
因為地圖中的值絕對不能限制為字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.