WebServer JSON响应更改对象的键名



我正在实现一个react/redux应用程序,在那里我调用API来获取一些数据。API是用f#编写的,并使用了Suave.io。在我的后端,对于一些API调用,我返回DataSet()。净类型)。它包括许多具有多个列的datatable。这些列的名称来自数据库。当我从API获取数据时,Suave。io将mime类型设置为JSON,因此它将数据集作为JSON对象传递给视图。视图中所有的数据都是正确的,只有DataTables的列名称被设置为来自数据库的名称。例如,如果名称是"ind . apple",那么在视图中它将是"ind . apple"。我不知道为什么会这样。

调用后端获取数据:

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);
        });
    };
}

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,或实际上覆盖甚至命名创建。

以上假设的问题是:"为什么要根据我没有阅读文档的内容来命名,我还错过了什么?"还是什么?"

最新更新