Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过';双倍';userLati、userLong值和字符串userName到Json变量/对象_Java_Json_Servlets - Fatal编程技术网

Java 如何通过';双倍';userLati、userLong值和字符串userName到Json变量/对象

Java 如何通过';双倍';userLati、userLong值和字符串userName到Json变量/对象,java,json,servlets,Java,Json,Servlets,在我的“PlayerLocation”servlet中,我得到了如下的“纬度、经度和名称” String userLatitude = request.getParameter("currentLat"); String userLongitude = request.getParameter("currentLong"); double userLati = Double.parseDouble(userLatitude); doubl

在我的“PlayerLocation”servlet中,我得到了如下的“纬度、经度和名称”

     String userLatitude = request.getParameter("currentLat");
     String userLongitude = request.getParameter("currentLong");
     double userLati = Double.parseDouble(userLatitude);         
     double userLong = Double.parseDouble(userLongitude);        
     DecimalFormat dFormat = new DecimalFormat("#.########");
     userLati= Double.valueOf(dFormat .format(userLati));
     userLong= Double.valueOf(dFormat .format(userLong));
     String userName = request.getParameter("name");         
我希望在JSP中接收纬度、经度和名称以及servlet响应。 如何将'double'userLati、userLong值和字符串userName传递给Json变量/对象

        String data =  ///???????
        String json = new Gson().toJson(data);          
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);

有人能告诉我如何做到这一点吗?

我想如果你能做到的话

class PlayerLocation {
 String currentLat;
 String currentLong;
 String name;

 public PlayerLocation(String currentLat, String currentLong, String name){
  this.currentLat = currentLat;
  this.currentLong = currentLong;
  this.name = name;
 }
}
然后更改字符串数据=/???对于新的PlayerLocation对象,可以使其看起来像:

PlayerLocation data = new PlayerLocation(/* data here */);
String json = new Gson().toJson(data);
我认为它应该起作用


我引用了以下内容:

您还可以通过以下方式使用
HashMap

HashMap map = new HashMap<String, Object>();
map.put("userLati", 23);
map.put("userLong", 45);
map.put("userName", "mr pro");
String json = new Gson().toJson(data);
HashMap map=newhashmap();
地图放置(“userLati”,23);
map.put(“userLong”,45);
地图放置(“用户名”、“mr pro”);
字符串json=new Gson().toJson(数据);

您应该对玩家详细信息进行pojo,并且字段应该是您希望在响应中使用的相应类型。以下是示例代码:

你的PlayerDetails类应该是

class PlayerDetails{
private String name;
private double latitude;
private double longitude;

    public PlayerDetails(String name, double latitude, double longitude) {
        this.name = name;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    //Getter and Setters
}
在servlet中,执行以下操作:

    PlayerDetails playerDetails=new 
                             PlayerDetails(userName,userLati,userLong);
    String jsonResponse = new Gson().toJson(playerDetails);
现在您可以将jsonResponse写入response。上述代码的示例响应:

{
  "name": "user1",
  "latitude": 51.503364,
  "longitude": -0.127625
}
PS:一个设计准则是您可以创建一个名为PlayerLocation的pojo,其中包含纬度和经度,并将其作为PlayerDetails的一部分。这会更干净。 在这种情况下,您的json响应将是

{
  "name": "user1",
  "location": {
    "latitude": 51.503364,
    "longitude": -0.127625
  }
}

这是个坏习惯。请不要用这种方式。@PriyaJain你能告诉我更多细节吗?为什么这是一种不好的做法?Java是一种面向对象的语言。创建一个映射并将东西放入其中确实是可行的解决方案,但与OO原则背道而驰。解决方案应该对类进行建模,而不是硬编码映射。