简体   繁体   English

使用Play Framework 2.2(Java)遍历JSON数组

[英]Iterate through JSON array with Play framework 2.2 (java)

Hi and thank you for reading. 嗨,谢谢您的阅读。

I have to call a restful service via the Play framework and parse the json result to an object list. 我必须通过Play框架调用静态服务,并将json结果解析为对象列表。 My problem is there are thousands of resources for creating a restful service with play but not much about the other direction. 我的问题是,有成千上万的资源可用于创建具有娱乐性的静态服务,但其他方向却不多。

My next problem is I'm only able to find examples of service calls in controllers. 我的下一个问题是我只能在控制器中找到服务调用的示例。 Is it a bad idea to call the service from a model? 从模型调用服务是否是一个坏主意? How would I call this service (without the need for a result)? 我怎么称呼这项服务(不需要结果)? This is what I have found so far. 这是我到目前为止发现的。

return async(
    WS.url("http://localhost:3021/Dashboard.svc/Conversation").get().map(
        new Function<WS.Response, Result>() {
            public Result apply(WS.Response response) {
                return ok(response.asJson());
            }
        }
    )
);

I'm new to Play, so forgive me if I ask stupid questions. 我是Play游戏的新手,所以如果我问一些愚蠢的问题,请原谅我。 I feel better with c# at the moment. 我现在对C#感觉更好。

Thanks in advance! 提前致谢! Ben

Found it. 找到了。 Or - found how to do it. 或者-找到了怎么做。 Not if it is a good idea to do how I did. 如果这样做是个好主意,那就不是。

import play.libs.WS;
import play.*;
import play.mvc.*;
import play.mvc.Result.*;
import play.libs.F.Promise;
import play.libs.F.Function;

import java.util.*;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class Order {

...

    public static List<Order> getOrders() {
        String hostUri = Helper.getWsHostUri();

        Promise<WS.Response> promise = WS.url(hostUri + "Orders").get();
        Promise<List<Order>> promisedResult = promise.map(
            new Function<WS.Response, List<Order>>() {
                public List<Order> apply(WS.Response response) {
                    JsonNode json = response.asJson();
                    ArrayNode results = (ArrayNode)json;

                    List<Order> orders = new ArrayList<Order>();
                    Iterator<JsonNode> it = results.iterator();

                    while (it.hasNext()) {
                        JsonNode node  = it.next();
                        Order order = new Order();

                        order.from = node.get("From").asText();
                        order.contact = node.get("Contact").asText();
                        order.amount = node.get("Amount").asDouble();
                        order.status = node.get("Status").asInt();

                        orders.add(order);
                    }

                    return orders;
                }
            }
        );

        return promisedResult.get();
    }
}

I would still be glad to see how to do it if you are not guessing like myself ... 如果您不像我自己那样猜测,我仍然很高兴看到如何做...

Thanks and have a nice day! 感谢,并有一个愉快的一天!

Ben

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

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