简体   繁体   English

将动态ArrayList转换为Json

[英]Converting Dynamic ArrayList to Json

I want to convert an array list to json string of a specific format. 我想将数组列表转换为特定格式的json字符串。 I get all the users emails in an array list and want to convert it into the following format of JSON. 我将所有用户电子邮件发送到数组列表中,并希望将其转换为以下JSON格式。

 [
  {"email":"abc@gmail.com"},
  {"email":"xyz@gmail.com"}
 ]

My controller action is 我的控制器动作是

 public static Result apiCustomers(){
    List<Customer> customerList = Model.coll(Customer.class).find().toArray();
    List<String> emails = new ArrayList<String>();

    for(Customer c : customerList){
        emails.add(c.email);
    }

    //ObjectNode result = Json.newObject();
    //result.put("emails", Json.toJson(emails));        
    return ok();
}

How can I convert the emails list to the above json format? 如何将电子邮件列表转换为上述json格式?

Thanks in advance 提前致谢

Why use another JSON ser/des lib? 为什么要使用另一个JSON ser / des lib? Play has one built-in (a wrapper around jackson - which is really fast). Play内置了一个(杰克逊周围的包装器-速度非常快)。

Starting from your code: 从您的代码开始:

public static Result apiCustomers(){
    List<Customer> customerList = Model.coll(Customer.class).find().toArray();
    List<String> emails = new ArrayList<String>();

    for(Customer c : customerList){
        emails.add(c.email);
    }

    return ok(Json.toJson(emails));
}

This uses some defaults but should be enough. 这使用一些默认值,但应该足够了。

Or manually: 或手动:

public static Result apiCustomers(){
        ArrayNode arrayNode = new ArrayNode(JsonNodeFactory.instance);

        List<Customer> customerList = Model.coll(Customer.class).find().toArray();

        for(Customer c : customerList){
            ObjectNode mail = Json.newObject();
            mail.put("email", c.email);
            arrayNode.add(mail);
        }

        return ok(arrayNode);
}

No need for Gson. 不需要Gson。

You can use this library: http://code.google.com/p/google-gson/ 您可以使用以下库: http : //code.google.com/p/google-gson/

Pretty easy tutorial about it here: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ 关于此的非常简单的教程: http : //www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Or you can write a custom toJson method to your classes or an util Json (not a big deal) 或者,您可以将自定义的toJson方法写入您的类或util Json(没什么大不了的)

in your case it should be something like that (i didn't test it): 在你的情况下应该是这样的(我没有测试):

public String toJson(List<String> emails) {
    StringBuilder result = new StringBuilder();
    result.append("[");
    result.append("\n");

    for (String s : emails) {
        result.append("{");
        result.append("\"email\":");
        result.append("\"");
        result.append(s);
        result.append("\"");
        result.append("}");
        result.append(",");
        result.append("\n");
    }
    result.append("]");
    return result.toString();
}

With the benefit of Java 8 (and I suspect a newer Jackson version): 受益于Java 8(我怀疑是Jackson的较新版本):

private static final ObjectMapper mapper = new ObjectMapper();

...

List<Customer> customerList = Model.coll(Customer.class).find().toArray();
ArrayNode emails = mapper.createArrayNode();
customerList.forEach(c -> emails.add(c.email));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM