我正在研究一个Java应用程序,我正在尝试从基于Spring-MVC的服务器和当前的system.out获取数据,但是我得到了输出,因为token是null。我打印出从服务器发送的代码,这是正确的。我没有那么多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();
}
}
}
CLient输出:
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}
我做错了什么,我想在客户端打印这个画布值。
答案 0 :(得分:1)
嗯, JsonToken.nextToken()的JsonParser API文档很好地说明了(强调我的):
主迭代方法,它将足够推进流以确定下一个标记的类型(如果有)。如果没有剩余(流在结束前没有可能的空格以外的内容),将返回 null 。
换句话说,如果没有剩余的令牌,则nextToken()将返回null。因此,由于您的 while 语句始终评估为 true ,因此它将在无限循环中继续打印令牌为空。
我应该提到的另一点是,如果你想要令牌 值 而不是 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 }
祝你好运!