Java : JsonPareser contains no data

我的梦境 提交于 2019-12-24 18:21:05

问题


I am working on a Java application in which I am trying to get data from a Spring-MVC based server and currently system.out it, but I am getting ouput as token is null. I printed out the code which is being sent from the server, it is correct. I don't have that much experience with REST, so I would appreciate if someone tells me what I am doing wrong.

Client code :

 public void getCanvas(){
           JsonFactory jsonFactory = new JsonFactory();
            String canvas = "";
            try {
                JsonParser jsonParser = jsonFactory.createJsonParser(new URL(canvasURL));
                JsonToken token = jsonParser.nextToken();
                while (token!=JsonToken.START_ARRAY){
                    token = jsonParser.nextToken();
                    System.out.println("Token is "+token);
                }
                while (token != JsonToken.END_ARRAY){
                    token = jsonParser.nextToken();
                    if(token == JsonToken.START_OBJECT){
                       canvas  = jsonParser.toString();
    // I also tried token.toString
                        System.out.println("Canvas is "+canvas);
                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

CLient output :

  Token is FIELD_NAME
Token is VALUE_NUMBER_INT
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_NUMBER_FLOAT
Token is FIELD_NAME
Token is VALUE_NULL
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_NULL
Token is FIELD_NAME
Token is VALUE_FALSE
Token is FIELD_NAME
Token is VALUE_NULL
Token is FIELD_NAME
Token is VALUE_NUMBER_INT
Token is END_OBJECT

That must have been printed atleast 100 times.

Server code :

  @RequestMapping(value = "/getcanvas",method = RequestMethod.GET)
    public @ResponseBody String getCanvasforFX(){
        System.out.println("Canvas was requested");
        Canvas canvas = this.canvasService.getCanvasById(10650);

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println("Canvas value is "+objectMapper.writeValueAsString(canvas));
            return objectMapper.writeValueAsString(canvas);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

Server output:

Canvas was requested
Canvas value is {"canvasid":10650,"canvasname":"Aks","canvasnumber":1.0,"canvastitle":null,"canvasdate":"","canvascreator":"","canvasiteration":"","canvasimage":"iVBORw0K5CYII=","canvasDisabled":false,"imageData":null,"person1id":7402}

What am I doing wrong, I want to print this canvas Value in the client-side.


回答1:


Well, the JsonParser API docs for JsonToken.nextToken() says it very well (emphasis mine):

Main iteration method, which will advance stream enough to determine type of the next token, if any. If none remaining (stream has no content other than possible white space before ending), null will be returned.

In other words, if there are no remaining tokens, nextToken() will return null. Accordingly, because your while statement always evaluates to true, it will keep on printing token is null in an endless loop.

Another point I should mention is that if you want the token value rather than the JsonToken.type, you should change

System.out.println("Token is "+ token);

to

System.out.println("Token is "+ jsonParser.getText());

otherwise, the code will print the following:

Token is FIELD_NAME
Token is VALUE_NUMBER_INT
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_NUMBER_FLOAT
Token is FIELD_NAME
Token is VALUE_NULL
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_STRING
Token is FIELD_NAME
Token is VALUE_FALSE
Token is FIELD_NAME
Token is VALUE_NULL
Token is FIELD_NAME
Token is VALUE_NUMBER_INT
Token is END_OBJECT

Therefore, your code should be modified as follows:

 public void getCanvas(){
       JsonFactory jsonFactory = new JsonFactory();
        String canvas = "";
        try {
            JsonParser jsonParser = jsonFactory.createJsonParser(new URL(canvasURL));
            JsonToken token = jsonParser.nextToken();
            while (token!=JsonToken.START_ARRAY && token != null){
                token = jsonParser.nextToken();

                // if we already passed last token,
                // break, don't print it out unnecessarily
                if (token == null) break;

                System.out.println("Token is "+ token);
            }
            while (token != JsonToken.END_ARRAY){
                token = jsonParser.nextToken();
                if(token == JsonToken.START_OBJECT){
                   canvas  = jsonParser.toString();
// I also tried token.toString
                    System.out.println("Canvas is "+canvas);
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

which yields the following output:

Token is canvasid
Token is 10650
Token is canvasname
Token is Aks
Token is canvasnumber
Token is 1.0
Token is canvastitle
Token is null
Token is canvasdate
Token is 
Token is canvascreator
Token is 
Token is canvasiteration
Token is 
Token is canvasimage
Token is iVBORw0K5CYII=
Token is canvasDisabled
Token is false
Token is imageData
Token is null
Token is person1id
Token is 7402
Token is }

Good luck to you!



来源:https://stackoverflow.com/questions/30237574/java-jsonpareser-contains-no-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!