gpt4 book ai didi

Java : JsonPareser contains no data

转载 作者:行者123 更新时间:2023-11-30 08:12:08 24 4
gpt4 key购买 nike

我正在开发一个Java应用程序,在该应用程序中我试图从基于Spring-MVC的服务器获取数据,当前是system.out它,但我得到的输出是因为 token 为空。我打印了从服务器发送的代码,它是正确的。我在 REST 方面没有太多经验,所以如果有人告诉我我做错了什么,我将不胜感激。

客户端代码:

 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();
}
}

}

客户端输出:

  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

该信息必须已被打印至少 100 次。

服务器代码:

  @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;
}
}

服务器输出:

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}

我做错了什么,我想在客户端打印这个 Canvas 值。

最佳答案

嗯,JsonToken.nextToken() 的 JsonParser API 文档说得很好(强调我的):

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.

换句话说,如果没有剩余 token ,nextToken() 将返回 null。因此,由于您的 while 语句的计算结果始终为 true,因此它将在无限循环中继续打印 token is null

我应该提到的另一点是,如果您想要 token 而不是JsonToken.type,则应该更改

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

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

否则,代码将打印以下内容:

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

因此,您的代码应修改如下:

 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();
}
}

产生以下输出:

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 }

祝你好运!

关于Java : JsonPareser contains no data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30237574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com