在玩游戏中获取json

时间:2016-02-26 07:18:57

标签: java playframework-2.4

我想从angularJS应用程序中获取带有我的play-framework应用程序()的json字符串。

这就是我实际发送的内容:

{"in":[
       {"id":4,"ip":"192.168.0.20","pinSysNo":4,"pinSysName":"pg6","folderName":"gpio4_pg6","alias":"d","direction":"digital_in"},
       {"id":3,"ip":"192.168.0.20","pinSysNo":3,"pinSysName":"pb18","folderName":"gpio3_pb18","alias":"c","direction":"digital_out"}
      ],
 "out":[
       {"id":1,"ip":"192.168.0.20","pinSysNo":1,"pinSysName":"pg3","folderName":"gpio1_pg3","alias":"a","direction":"digital_in"},
       {"id":2,"ip":"192.168.0.20","pinSysNo":2,"pinSysName":"pb16","folderName":"gpio2_pb16","alias":"b","direction":"digital_in"}
            ]
}:""

我总是得到状态码500(内部服务器错误)。 可能我的路线中有些不正确吗? 路线:

POST        /networkInsertJson          controllers.NetworkController.InsertJson()

我可能会对json字符串中的最后3个字符有问题吗?

这是我的控制器:

public static Result InsertJson(){
    ObjectNode result = Json.newObject();
    JsonNode json = request().body().asJson();
    org.json.simple.parser.JSONParser parser = new JSONParser();
    JSONObject o = new JSONObject();
    try{
        o = (JSONObject) parser.parse(json.asText());
    }catch (Exception e){

    }

    JSONArray ins = (JSONArray) o.get("in");
    JSONArray outs = (JSONArray) o.get("out");

    //I return the json back for testing
    result.put("in", ins.toJSONString());
    return ok(result);
}
你帮我找错了吗?

1 个答案:

答案 0 :(得分:1)

我在这些方面发现了自己的错误:

result.put("in", ins.toJSONString());

我尝试插入一个数组作为json元素的内容。