Java remove "parent":"somevalue" from flattened json if “parent”:“somevalue” and there also exists a “parent.new”: “somevalue” in same json

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

Java remove "parent":"somevalue" from flattened json if “parent”:“somevalue” and there also exists a “parent.new”: “somevalue” in same json

问题

{
"lastModifiedBy.$oid": "1234567189",
"displayedBy.one.new": "Sammy",
"displayedBy.two": "random_value3",
"a.b": null,
"b.c": null,
"d.e.f": null
}

是的,在Jackson或其他Java包中是有方法可以做到这一点的。

英文:

I Have json like this :

{
"lastModifiedBy" :"value",
"lastModifiedBy.$oid": "1234567189",
"displayedBy" : 0,
"displayedBy.one" : "Abhi",
"displayedBy.one.new" : "Sammy",
"displayedBy.two":"random_value3",
"a.b":null,
"b.c":null,
"d.e.f":null
}

I only want to keep the longest keys and not the previous state parent without affecting the other keys
output I need:

{
"lastModifiedBy.$oid": "1234567189",
"displayedBy.one.new" : "Sammy",
"displayedBy.two":"random_value3",
"a.b":null,
"b.c":null,
"d.e.f":null
}

Is there a way to do this in Jackson or any other java package.?

答案1

得分: 1

如果您能将其转换为ArrayList,请删除所有不需要的键。例如,使用foreach。

如果您能将其转换为以":"和","分割的字符串,删除"s,并创建一个ArrayList,然后使用索引0,2,4...作为键,1,3,5...作为值填充它,并执行上述提到的操作。

英文:

If you are able to convert it into an ArrayList, remove all keys you dont need. For example with foreach.

If you are able to convert it into an String split at ":" and "," remove the "s create an ArrayList and populate it by using index 0,2,4... as key and 1,3,5... as value and do the thing mentioned above.

答案2

得分: 0

请尝试这样做。

Map<String, String> map = new HashMap<>();
map.put("lastModifiedBy", "value");
map.put("lastModifiedBy.$oid", "1234567189");
map.put("displayedBy", "0");
map.put("displayedBy.one", "Abhi");
map.put("displayedBy.one.new", "Sammy");
map.put("displayedBy.two", "random_value3");
map.put("a.b", null);
map.put("b.c", null);
map.put("d.e.f", null);
List<String> removes = new ArrayList<>();
for (String parent : map.keySet())
    for (String child : map.keySet())
        if (parent != child && child.startsWith(parent)) {
            removes.add(parent);
            break;
        }
for (String key : removes)
    map.remove(key);
System.out.println(map);

输出:

{a.b=null, d.e.f=null, displayedBy.one.new=Sammy, b.c=null, displayedBy.two=random_value3, lastModifiedBy.$oid=1234567189}
英文:

Try this.

    Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
    map.put(&quot;lastModifiedBy&quot;, &quot;value&quot;);
    map.put(&quot;lastModifiedBy.$oid&quot;, &quot;1234567189&quot;);
    map.put(&quot;displayedBy&quot;, &quot;0&quot;);
    map.put(&quot;displayedBy.one&quot;, &quot;Abhi&quot;);
    map.put(&quot;displayedBy.one.new&quot;, &quot;Sammy&quot;);
    map.put(&quot;displayedBy.two&quot;, &quot;random_value3&quot;);
    map.put(&quot;a.b&quot;, null);
    map.put(&quot;b.c&quot;, null);
    map.put(&quot;d.e.f&quot;, null);
    List&lt;String&gt; removes = new ArrayList&lt;&gt;();
    for (String parent : map.keySet())
        for (String child : map.keySet())
            if (parent != child &amp;&amp; child.startsWith(parent)) {
                removes.add(parent);
                break;
            }
    for (String key : removes)
        map.remove(key);
    System.out.println(map);

output:

{a.b=null, d.e.f=null, displayedBy.one.new=Sammy, b.c=null, displayedBy.two=random_value3, lastModifiedBy.$oid=1234567189}

huangapple
  • 本文由 发表于 2020年9月3日 19:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63722870.html
匿名

发表评论

匿名网友

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

确定