[英]How to convert List<Object> to JSON on playframework 2.1 Java
I have the following problem. 我有以下问题。 How can I convert the list of objects to JSON?
如何将对象列表转换为JSON?
I try this: 我试试这个:
List<PLZ> plzs = PLZ.findPlz(plz);
String json = play.libs.Json.toJson(plzs);
but I get the following error message: incompatible types 但我收到以下错误消息:不兼容的类型
[Autocomplete] $ compile
[info] Compiling 1 Java source to C:\Entwicklungsumgebung\play-2.1.3\Autocomplete\target\scala-2.10\classes...
[error] C:\Entwicklungsumgebung\play-2.1.3\Autocomplete\app\controllers\Application.java:25: error: incompatible types
[error] String json = play.libs.Json.toJson(plzs);
[error] ^
[error] required: String
[error] found: JsonNode
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code
[error] Total time: 1 s, completed 05.09.2013 13:58:08
what am I doing wrong? 我究竟做错了什么?
and how can I convert the list of objects to JSON? 以及如何将对象列表转换为JSON?
package models;
import java.util.*;
import javax.persistence.*;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;
@Entity
public class PLZ extends Model {
@Id
public Long id;
public String plz;
public String beschreibung1;
public String beschreibung2;
public static Finder<Long,PLZ> find = new Finder(Long.class, PLZ.class);
public static List<PLZ> findPlz(String plz){
List<PLZ> plzs = find.where().ilike("plz", plz+"%").findList();
return plzs;
}
}
package controllers;
import play.libs.Json;
import java.util.*;
import play.*;
import play.mvc.*;
import models.*;
import views.html.*;
public class Application extends Controller {
@BodyParser.Of(play.mvc.BodyParser.Json.class)
public static Result findPlz(String plz) {
List<PLZ> plzs = PLZ.findPlz(plz);
String json = play.libs.Json.toJson(plzs);
return ok(json);
}
}
sorry, I defeniere false return-type 对不起,我defeniere假返回类型
rather than 而不是
String json = play.libs.Json.toJson(plzs);
must be 一定是
org.codehaus.jackson.JsonNode json = Json.toJson(plzs);
import net.sf.json.JSONObject;
....
String resultString = "";
for (QAAsset qaAsset : assetList) {
JSONObject jsonObject = JSONObject.fromObject(qaAsset);
resultString += "," + jsonObject.toString();
}
renderJSON("[" + resultString.substring(1) + "]");
I use this way to convert the list of objects to JSON. 我用这种方式将对象列表转换为JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.