简体   繁体   English

F#:保存JSON数据

[英]F#: Saving JSON data

I am new to programming and F# is my first language. 我是编程新手,F#是我的第一语言。

Here are the relevant parts of my code: 这是我的代码的相关部分:

let internal saveJsonToFile<'t> (someObject:'t) (filePath: string) =  
    use fileStream = new FileStream(filePath, FileMode.OpenOrCreate) 
    (new DataContractJsonSerializer(typeof<'t>)).WriteObject(fileStream, someObject)

let testList = new List<Fighter>()

saveJsonToFile testList<Fighter> @"G:\User\Fighters.json"

I have previously created an F# record of type Fighter. 我以前创建了Fighter类型的F#记录。

When I try to run "saveJsonToFile testList @"G:\\User\\Fighters.json"", "testList" is underscored in red with the error message: "Unexpected type arguments". 当我尝试运行“ saveJsonToFile testList @” G:\\ User \\ Fighters.json“”时,“ testList”用红色强调并带有错误消息:“意外类型参数”。

What went wrong? 什么地方出了错? What am I missing? 我想念什么?

First, your testList is a value , not a function. 首先,您的testList是一个 ,而不是一个函数。
Second, it doesn't have a generic argument, everything in its body is concrete. 其次,它没有通用的论点,它体内的一切都是具体的。

If you want it to be a function, give it a parameter. 如果要使其成为函数,请为其提供参数。
If you want it to be generic, say so in the body. 如果您希望它是通用的,请在正文中说。

let testList () = new List<'a>()
saveJsonToFile testList<Fighter>() @"G:\\User\\Fighters.json"

And finally, the List you're trying to create probably resolves to F#'s own List , which is a module, not a type. 最后,您要创建的List可能解析为F#自己的List ,它是一个模块,而不是类型。
If you meant to create the .NET's System.Collections.Generic.List , then you need to specify the full type name: 如果要创建.NET的System.Collections.Generic.List ,则需要指定完整的类型名称:

let testList () = new System.Collections.Generic.List<'a>()

But if you meant to create an F# native list, use one of its constructors: 但是,如果要创建F#本机列表,请使用其构造函数之一:

let testList () = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM