如何将嵌套的 HashMap 转换为 Jettison JSONObject

huangapple go评论61阅读模式
英文:

How to convert Nested HashMap to Jettison JSONObject

问题

I have to convert a Map<String, Map<String, String>> to Codehaus-Jettison JSONObject.

I'm aware that using Gson and other libraries have easier ways of achieving this, but its a requirement hat Jettison be used in this case.

What I understand from the documentation is I could do:

Map<String, Map<String, String>> tagsMap = new HashMap<>();
Map<String, String> tags = new HashMap<>();
tags.put("tag1", "value1");
tags.put("tag2", "value2");
tags.put("tag3", "value3");

tagsMap.put("table1", tags);
tagsMap.put("table2", tags);
tagsMap.put("table3", tags);

JSONObject jsonObject = new JSONObject(tagsMap);

System.out.println(jsonObject.toString());

But the new JSONObject(map) only seems to be working for Map<String, String> and for the above code I end up with this incorrect output:

{"table3":"{tag1=value1, tag2=value2, tag3=value3}","table2":"{tag1=value1, tag2=value2, tag3=value3}","table1":"{tag1=value1, tag2=value2, tag3=value3}"}

My desired output should be proper JSON content, like this:

{"table3":{"tag1":"value1", "tag2":"value2", "tag3":"value3"},"table2":{"tag1":"value1", "tag2":"value2", "tag3":"value3"},"table1":{"tag1":"value1", "tag2":"value2", "tag3":"value3"}}

Is there any way of doing this with at all ONLY Jettison?

英文:

I have to convert a Map<String, Map<String, String>> to Codehaus-Jettison JSONObject.

I'm aware that using Gson and other libraries have easier ways of achieving this, but its a requirement hat Jettison be used in this case.

What I understand from the documentation is I could do:

Map&lt;String, Map&lt;String, String&gt;&gt; tagsMap = new HashMap&lt;&gt;();
Map&lt;String, String&gt; tags = new HashMap&lt;&gt;();
tags.put(&quot;tag1&quot;, &quot;value1&quot;);
tags.put(&quot;tag2&quot;, &quot;value2&quot;);
tags.put(&quot;tag3&quot;, &quot;value3&quot;);

tagsMap.put(&quot;table1&quot;, tags);
tagsMap.put(&quot;table2&quot;, tags);
tagsMap.put(&quot;table3&quot;, tags);

JSONObject jsonObject = new JSONObject(tagsMap);

System.out.println(jsonObject.toString());

But the new JSONObject(map) only seems to be working for Map<String, String> and for the above code I end up with this incorrect output:

{&quot;table3&quot;:&quot;{tag1=value1, tag2=value2, tag3=value3}&quot;,&quot;table2&quot;:&quot;{tag1=value1, tag2=value2, tag3=value3}&quot;,&quot;table1&quot;:&quot;{tag1=value1, tag2=value2, tag3=value3}&quot;}

My desired output should be proper JSON content, like this:

{&quot;table3&quot;:{&quot;tag1&quot;:&quot;value1&quot;, &quot;tag2&quot;:&quot;value2&quot;, &quot;tag3&quot;:&quot;value3&quot;},&quot;table2&quot;:{&quot;tag1&quot;:&quot;value1&quot;, &quot;tag2&quot;:&quot;value2&quot;, &quot;tag3&quot;:&quot;value3&quot;},&quot;table1&quot;:{&quot;tag1&quot;:&quot;value1&quot;, &quot;tag2&quot;:&quot;value2&quot;, &quot;tag3&quot;:&quot;value3&quot;}}

Is there any way of doing this with at all ONLY Jettison ?

答案1

得分: 1

似乎您正在使用较旧版本的 jettison,它在 jettison 版本 1.3 及更高版本上运行良好。升级库的版本,它将正常工作。

英文:

Seems you are using older version of jettison, its working fine on jettison version 1.3 and later. Upgrade the library version and it will work fine.

huangapple
  • 本文由 发表于 2020年9月17日 16:08:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定