Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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 如何获取Json对象并将其存储在Hashmap中_Java_Servlets - Fatal编程技术网

Java 如何获取Json对象并将其存储在Hashmap中

Java 如何获取Json对象并将其存储在Hashmap中,java,servlets,Java,Servlets,在我的dopost中,我试图从请求中获取一个Json对象,然后使用fromJson从该请求中创建一个Profile对象,然后将其存储在我的hashmap中。当我尝试在代码中这样做并打印出hashmap时,我刚刚创建的新配置文件只打印出{id=0},但我硬编码的所有其他配置文件都可以打印出来。我的新档案没有打印出我的用户名、姓氏、年龄等 有人知道我做错了什么吗 我试图以json的形式发送的内容 { "4": { "id": "9", "username": "gfgf",

在我的dopost中,我试图从请求中获取一个Json对象,然后使用fromJson从该请求中创建一个Profile对象,然后将其存储在我的hashmap中。当我尝试在代码中这样做并打印出hashmap时,我刚刚创建的新配置文件只打印出{id=0},但我硬编码的所有其他配置文件都可以打印出来。我的新档案没有打印出我的用户名、姓氏、年龄等

有人知道我做错了什么吗

我试图以json的形式发送的内容

{
"4": {
    "id": "9",
    "username": "gfgf",
    "lastname": "hgfh",
    "favTeam": "Manc city",
    "age": "51"
}
}
我的代码

public class ProfileServlet extends HttpServlet 
{
protected HashMap<Integer, Profile> team = new HashMap<Integer, Profile>();

private static final long serialVersionUID = 1L;
private Gson gson = new Gson();

public ProfileServlet() 
{

    Profile profile1 = new Profile(1,"bob","bee","Manc city","21");
    Profile profile2 = new Profile(2,"billy","smith","Dortmud","25");
    Profile profile3 = new Profile(3,"john","jamesd","Aston Villa","44");

    int id = 1;
    int id2 = 2;
    int id3 = 3;

    team.put(id,profile1);
    team.put(id2,profile2); 
    team.put(id3,profile3);
}

public void sendAsJson(HttpServletResponse response, Object obj) throws IOException
{
    PrintWriter out = response.getWriter(); 
    response.setContentType("application/json; charset=UTF-8");

    String jsonString = gson.toJson(obj);


    out.print(jsonString);
    out.flush();

}

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
    response.setContentType("application/json; charset=UTF-8"); 
    int counter = 0;
    boolean breakloop = false;
    PrintWriter out = response.getWriter();

    String path = request.getPathInfo();



    if(path == null || path.equals("/"))
    {
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();

        String line;
        while ((line = reader.readLine()) != null)
        {
            buffer.append(line);
        }

        String myprofile = buffer.toString();
        Profile newp = gson.fromJson(myprofile, Profile.class);

        //loop to create my IDs in order
        while(breakloop == false)
        {
            if(!team.containsKey(counter))
            {
                newp.id = counter;
                breakloop = true;
            }
            else
                counter++;          

        }       

        team.put(newp.id,newp);
        sendAsJson(response,team);

    }
    else
    {
        response.sendError(400, "Incorrect request, make sure you typed the body correctly!");
    }



}

}
公共类ProfileServlet扩展了HttpServlet { 受保护的HashMap团队=新建HashMap(); 私有静态最终长serialVersionUID=1L; private Gson Gson=new Gson(); 公共配置文件servlet() { Profile Profile 1=新的Profile(1,“bob”、“bee”、“Manc city”、“21”); Profile profile2=新的Profile(2,“比利”、“史密斯”、“多特蒙德”、“25”); Profile Profile 3=新的Profile(3,“john”、“jamesd”、“Aston Villa”、“44”); int-id=1; int id2=2; int id3=3; 团队投入(id,档案1); 团队投入(id2,profile2); 团队。put(id3,profile3); } public void sendAsJson(HttpServletResponse,Object obj)引发IOException { PrintWriter out=response.getWriter(); setContentType(“application/json;charset=UTF-8”); 字符串jsonString=gson.toJson(obj); 打印输出(jsonString); out.flush(); } 受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException { setContentType(“application/json;charset=UTF-8”); int计数器=0; 布尔breakloop=false; PrintWriter out=response.getWriter(); 字符串路径=request.getPathInfo(); if(path==null | | path.equals(“/”) { StringBuilder缓冲区=新的StringBuilder(); BufferedReader reader=request.getReader(); 弦线; 而((line=reader.readLine())!=null) { buffer.append(行); } 字符串myprofile=buffer.toString(); Profile newp=gson.fromJson(myprofile,Profile.class); //循环以按顺序创建我的ID while(breakloop==false) { 如果(!团队容器(计数器)) { newp.id=计数器; breakloop=true; } 其他的 计数器++; } team.put(newp.id,newp); sendAsJson(响应,团队); } 其他的 { sendError(400,“请求不正确,请确保键入的正文正确!”); } } }
我解决了我的问题,我的代码没有问题,但问题出在我的json请求中。这对我有用

我的Json是如何工作的,我想添加那些外括号并没有得到我的完整请求

{
  "id": "9",
  "username": "gfgf",
  "lastname": "hgfh",
  "favTeam": "Manc city",
  "age": "51"
}