WebServer JSON 对象的响应更改键名称

WebServer JSON Response change key names for Object

我正在实施一个 react/redux 应用程序,我在其中调用 API 来获取一些数据。 API 是用 F# 编写的,使用 Suave.io。在我的后端,对于一些 API 调用,我 return 一个数据集(.NET 类型)。它包括许多具有多列的数据表。这些列的名称来自数据库。当我从 API 获取数据时,Suave.io 将 mime 类型设置为 JSON,因此它采用数据集并将它们作为 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);
        });
    };
}

文雅 API 代码片段:

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

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

我认为这里有几个问题...有点...而且我认为 react/redux 是为了 而不是 像您那样提取数据,而是推送数据.

除此之外:我猜:

jsonSerializerSettings.ContractResolver <- new CamelCasePropertyNamesContractResolver()

是相关的。

所以通过阅读文档 f.i。 http://www.newtonsoft.com/json/help/html/contractresolver.htm 我认为可以选择 CamelCasePropertyNamesContractResolver 之外的其他名称,或者甚至覆盖创建的命名。

以上假设问题是:"Why is the naming according to what I didnt bother to read up on documentation, and what else have I missed? Or something?"