请提供有关在java端编写json对象比较的线索
我计划在 java 端比较两个 json 对象。每个服务的 json 对象结构可能有所不同。所以,我想编写json对象比较的通用代码。我无法使用 Jackson 或任何其他库/API。因为,我需要获得批准才能使用。这比我自己写java代码会更有负担。所以,请给出想法。如何编写 json 对象比较? 并且,请提供示例代码库 提前致谢
I am planning to compare two json objects at java side. each service json object structure may vary. So, I want to write common code for json object comparision. I cant use Jackson or any other library/API. because, I need to get approval to use. It will be more burden rather than writing my own java coding. So, Please give idea. How can i write json object comparision?
And, Please provide sample code base
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看参考实现并提取您需要的部分。
Take a look at the reference implementation and extract the parts you need.
我假设您从一些专有或内部类开始在内存中表示 JSON,并且该表示基于哈希表/哈希映射或类似的东西。
进行比较的方法是将名称/值对从 JSON 对象树递归复制到使用 TreeMaps 构建的树中。然后,您可以递归地遍历/迭代两个版本的 JSON 的 TreeMaps 树。
或者将属性名称提取到 TreeSet 中,迭代它们以按一致的顺序提供属性名称。
我自己写过这样的东西。 IIRC,它正在我的一个 GPL 项目的单元测试中。哎呀!!抱歉 :-)
FWIW - 您应该告诉/提醒您的管理层他们的开源使用政策为您带来了多少额外工作...并调整您的项目估算以考虑到这一点。
FWIW 2 - 从网络上随机的某个人那里获取示例代码比使用已发布的开源库风险更大(从 IP 角度来看)。至少对于该库,您知道它来自哪里以及适用的许可条件。 (有人可以向您发送从您最喜欢的软件垄断者那里窃取的专有代码。我敢打赌您的律师一定会喜欢的!)
I'm assuming that you are starting from some proprietary or in-house classes for representing JSON in memory, and that the representation is based on hash tables / hash maps or some such.
The way to do the comparison is to recursively copy the name-value pairs from the JSON object trees into a tree built using TreeMaps. Then you can recursively walk / iterate the tree of TreeMaps of the two versions of the JSON.
Alternatively extract the attribute names into TreeSets, iterate those to give you the attribute names in a consistent order.
I've written stuff like this myself. IIRC, it is in the unit tests for one of my GPL'ed projects. Ooops!! Sorry :-)
FWIW - you should be telling / reminding your management how much extra work their Open Source usage policies are making for you ... and adjusting your project estimates to take this into account.
FWIW 2 - grabbing sample code from some random person on the net is more risky (from an IP perspective) than using a published open source library. At least with the library, you know where it comes from and what license conditions apply. (Someone could send you proprietary code stolen from your favourite software monopolist. I bet your lawyers would just LOVE that!)