- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个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/
我正在开发一个Java应用程序,在该应用程序中我试图从基于Spring-MVC的服务器获取数据,当前是system.out它,但我得到的输出是因为 token 为空。我打印了从服务器发送的代码,它是正
我是一名优秀的程序员,十分优秀!