WebServer JSON Response更改Object的键名

时间:2016-09-06 22:57:29

标签: reactjs f# redux suave

我正在实施一个react / redux应用,我正在调用API来获取一些数据。 API是用F#编写的,并使用Suave.io。在我的后端,对于一些API调用,我返回一个DataSet(.NET类型)。它包含许多具有多列的DataTable。这些专栏'名称来自数据库。当我从API获取数据时,Suave.io将mime类型设置为JSON,因此它接受DataSet并将它们作为json对象传递给视图。视图具有正确的所有数据,但DataTables的列名称设置为数据库中的名称。例如,如果名称是" IND.APPL"那么在视图中它将是" inD.APPL"。我不知道为什么会这样。

呼叫后端获取数据:

export function loadIndicesDataAPI(data) {
    return function(dispatch) {
        dispatch(beginAjaxCall());
        return fetch(`http://localhost:8083/indices`, {
            method: 'post',
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
        })
        .then(response => {
            return response.json();
        })
        .then(dataRes => {
            dispatch(loadIndicesDataSuccess(dataRes));
        }).catch(error => {
            dispatch(ajaxCallError(error))
            throw(error);
        });
    };
}

Suave API code snippets:

type indicesForm = JsonProvider<""" { "data": [{ "shortName": "s", "name": "n", "symbolId": 1, "isAdded": true, "isDatabase": true, "isReturn": false }], "startdate": "2010-01-01", "enddate": "2011-01-01", "rebalance": 0, "observationAnalysis": 1 } """>

[<AutoOpen>]
module RestFul =

  type RestInit = {
    RefreshAPI : IndexItem [] * DateTime * DateTime * int * int -> DataSet
  } 

let JSON v =
  let jsonSerializerSettings = new JsonSerializerSettings()
  jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

  JsonConvert.SerializeObject(v, jsonSerializerSettings)
  |> OK
  >=> Writers.setMimeType "application/json; charset=utf-8"

let fromJson<'a> json =
  let indicesFormJson = indicesForm.Parse(json)
  let indexItems = Array.init (indicesFormJson.Data.Length) (fun ind -> 
    let data = indicesFormJson.Data.[ind]
    let indexItemNew = new IndexItem(data.SymbolId, data.Name, data.ShortName, data.IsReturn)
    if data.IsAdded then indexItemNew.Add()
    if data.IsDatabase then indexItemNew.Imported()
    indexItemNew)
  let startDate = indicesFormJson.Startdate
  let endDate = indicesFormJson.Enddate
  let rebalance = indicesFormJson.Rebalance
  let observationAnalysis = indicesFormJson.ObservationAnalysis
  indexItems, startDate, endDate, rebalance, observationAnalysis

let getResourceFromReq<'a> (req : HttpRequest) =
  let getString rawForm =
    System.Text.Encoding.UTF8.GetString(rawForm)
  req.rawForm |> getString |> fromJson<'a>

let restInit resourceName resource =
  let resourcePath = "/" + resourceName
  path resourcePath >=> choose [
    POST >=> request (getResourceFromReq >> resource.RefreshAPI >> JSON)
  ]


[<EntryPoint>]
let main argv =
  let indicesWebPart = restInit "indices" {
    RestInit.RefreshAPI = RefreshAPI 
  }

  startWebServer defaultConfig (choose [indicesWebPart])
  0

另一件事是列名以大写字母开头,在前端变为小写。

1 个答案:

答案 0 :(得分:1)

我认为这里有几个问题......有点......而且我认为react / redux用于像你一样拉动数据,但推送数据。

除此之外:我猜:

jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

是相关的。

所以通过阅读doucmentation f.i. http://www.newtonsoft.com/json/help/html/contractresolver.htm我认为可以选择另一个而不是CamelCasePropertyNamesContractResolver,或者甚至覆盖创建的命名。

所有上述假设问题是:&#34;为什么命名根据我没有费心阅读文档,还有什么我错过了?还是什么?&#34;