简体   繁体   English

我可以在Java中使用JSONObject代替HashMap吗?

[英]Can I use JSONObject instead of HashMap in Java?

I am doing an application which has a server and web, native clients. 我正在做一个具有服务器和Web本机客户端的应用程序。 Whenever a request comes from any of the client server should do the following: 每当来自任何客户端服务器的请求都应执行以下操作:

  1. Fetch the data/model 获取数据/模型
  2. For webclient request create a html page by compiling the corresponding JSP page or else for native-client request simply fetches the data alone. 对于webclient请求,通过编译相应的JSP页面来创建html页面,否则,对于native-client请求,只需简单地获取数据即可。

Generally I fetch the data and put it into an HashMap and pass it to the JSP page. 通常,我获取数据并将其放入HashMap并将其传递到JSP页面。 But for native requests I am planning to return the data in json format. 但是对于本机请求,我计划以json格式返回数据。 Here my doubt is instead of having data in different objects( HashMap and JSONObject ), can I use the JSONObject for both purposes?. 在这里我的疑问是不是可以将数据存储在不同的对象( HashMapJSONObject )中,我可以同时使用JSONObject吗? Will there be any performance implications when I use JSONObject instead of HashMap ?. 当我使用JSONObject代替HashMap时,会对性能产生影响吗?

There will be not that much differentce, because most implementations of JSON are backed by maps. 差别不会太大,因为大多数JSON实现都由地图支持。 Better solution ( from JSP point of view ) would be using java beans, and convert your JSON / other data source to objects using some databinding solution 更好的解决方案(从JSP角度来看)将使用Java Bean,并使用某些数据绑定解决方案将JSON /其他数据源转换为对象

从软件工程的角度来看,不应在其他层(例如示例中的持久性层)中使用特定于视图的数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM