Erlang Chicagoboss无法获得正确的JSON响应

时间:2017-07-23 18:29:30

标签: json erlang chicagoboss

在我的控制器文件中,我有一个方法可以读取传入的HTTP请求,从数据库中读取用户数据,使用JSON编码结果(使用jsx)并将其作为响应发送。

    sensorusersdetails('GET', []) ->
        Headers = [{'Access-Control-Allow-Origin', "*"},
        {'Access-Control-Allow-Methods',  "GET, OPTIONS"},
        {'Content-Type',  "application/json"},
        {'Access-Control-Allow-Headers', "X-Requested-With"},
        {'Access-Control-Max-Age', "180"}],   
        Building =  Req:query_param("bld"),
        io:format("User Data request from Node.js server~n~p~n", 
        [Req:query_params()]),
        {{Year,Month,Day},{_,_,_}} = erlang:localtime(),
        StrDate = lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",
        [Year,Month,Day])),
        BUserDataList = boss_db:find(sensoruser_data, [{building, 'equals', Building}]),
        io:format("Current Users Data stored in the database: ~n~p~n",[BUserDataList]),

        MyUserJSONList = sensor_preapre_data(BUserDataList, StrDate),
        io:format("The Present Date Sensor Users Data with Binary 1: ~n~p~n",[MyUserJSONList]),
        MyUserJSONListLength = length(MyUserJSONList),
        if MyUserJSONListLength > 0 ->     
            MyFinalList = sensor_data_final(MyUserJSONList),
            io:format("The Present Date Sensor Users Data without Binary 2: ~n~p~n",[MyFinalList]),
            {200, [MyFinalList], Headers};
            %%{json, MyFinalList};
        true ->
            {200, "NO DATA FOUND", Headers}
            %%{json, [{error, "NO DATA FOUND"}]}
        end.        

在Chicagoboss服务器日志中,我得到了:

The Present Date Sensor Users Data with Binary 1: 
[[<<"{\"username\":\"KPBatman1\",\"building\":\"A\",\"device\":\"Fitbit\",\"date\":\"2017-07-23\",\"calorie\":732,\"distance\":6.4399999999999995,\"elevation\":0,\"floor\":0,\"steps\":8}">>],
 [<<"{\"username\":\"KPSuperman1\",\"building\":\"A\",\"device\":\"Jawbone\",\"date\":\"2017-07-23\",\"calorie\":0,\"distance\":0.0,\"elevation\":0,\"floor\":0,\"steps\":0}">>]]

The Present Date Sensor Users Data without Binary 2: 
[["{\"username\":\"KPBatman1\",\"building\":\"A\",\"device\":\"Fitbit\",\"date\":\"2017-07-23\",\"calorie\":732,\"distance\":6.4399999999999995,\"elevation\":0,\"floor\":0,\"steps\":8}"],
 ["{\"username\":\"KPSuperman1\",\"building\":\"A\",\"device\":\"Jawbone\",\"date\":\"2017-07-23\",\"calorie\":0,\"distance\":0.0,\"elevation\":0,\"floor\":0,\"steps\":0}"]]

然而,当我发送HTTP请求时 - 我得到的JSON响应:

{"username":"KPBatman1","building":"A","device":"Fitbit","date":"2017-07-23","calorie":732,"distance":6.4399999999999995,"elevation":0,"floor":0,"steps":8}
{"username":"KPSuperman1","building":"A","device":"Jawbone","date":"2017-07-23","calorie":0,"distance":0.0,"elevation":0,"floor":0,"steps":0}

发送JSON响应的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

  

但是,当我发送HTTP请求时 - 我是JSON响应   得到:

public class Messages : Controller 
{
     public ActionResult MessagesGridView()
     {
         // grid view populating data code lines here
         return PartialView("_GridView", data);
     }

     public ActionResult Load(int id)
     {
         // code lines to find ID here
         return PartialView("_ModalPopup", model);
     }
}

和?你期望/想要得到什么?

以下代码 对我来说因为输出是我 期望 看到的:

{"username":"KPBatman1","building":"A", ...}
{"username":"KPSuperman1","building":"A", ...}

在我的浏览器中,我看到了:

-module(cb_tutorial_greeting_controller, [Req]).
-compile(export_all).

hello('GET', []) ->
    Headers = [
        {'Access-Control-Allow-Origin', "*"},
        {'Access-Control-Allow-Methods',  "GET, OPTIONS"},
        {'Content-Type',  "application/json"},
        {'Access-Control-Allow-Headers', "X-Requested-With"},
        {'Access-Control-Max-Age', "180"}
    ],   
    Data = [
        [<<"{\"username\":\"KPBatman1\",\"building\":\"A\"}">>],
        [<<"{\"username\":\"KPSuperman1\",\"building\":\"A\"}">>]
    ],
    Json = jsx:encode(Data),
    {200, Json, Headers}.

请注意,MyFinalList甚至不是有效的JSON:

[["{\"username\":\"KPBatman1\",\"building\":\"A\"}"],["{\"username\":\"KPSuperman1\",\"building\":\"A\"}"]]

看看我在那里做了什么?