[英]JSONObject.toString Type mismatch cannot convert from JSONString to JSONObject
Per the API we should be able to do this. 根据API,我们应该能够做到这一点。
http://www.json.org/javadoc/org/json/JSONObject.html#toString() http://www.json.org/javadoc/org/json/JSONObject.html#toString()
@Override
public JSONObject buildPayload(BuildData buildData, String jenkinsUrl, List<String> logLines) {
JSONObject payload = new JSONObject();
payload.put("data", buildData.toJson());
payload.put("message", logLines);
payload.put("source", "jenkins");
payload.put("source_host", jenkinsUrl);
payload.put("@timestamp", buildData.getTimestamp());
payload.put("@version", 1);
// we need to flatten payload from JSONObject to String
return payload.toString();
}
Clearly, we have defined payload and it is a JSONObject. 显然,我们已定义了有效负载,它是一个JSONObject。 Why isn't this working and what should be done?
为什么这不起作用,应该做什么?
Your method declares that it returns a value of type JSONObject
. 您的方法声明它返回
JSONObject
类型的值。 But this: 但是这个:
return payload.toString();
returns a value of type String
. 返回
String
类型的值。 There's no implicit conversion from String
to JSONObject
, hence the compile-time error. 没有从
String
到JSONObject
隐式转换,因此编译时错误。
If you really want a string, change the method return type. 如果您确实需要字符串,请更改方法返回类型。 If you really want a
JSONObject
, just change the return statement to: 如果你真的想要一个
JSONObject
,只需将return语句改为:
return payload;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.