gpt4 book ai didi

c# - 使用类型提供程序和 JSON API

转载 作者:行者123 更新时间:2023-11-30 17:34:39 26 4
gpt4 key购买 nike

我正在尝试使用返回 JSON 构建的 API :

Team.player1/player2/player3 ...(球员是作为属性而不是数组构建的)

我想用反射来做,但是很难找到播放器。

type Simple = JsonProvider<"sample.json">

let wbReq = "API-FOR-TEAM"

let docAsync = Simple.Load(wbReq)

let allValues = FSharpType.GetRecordFields (docAsync.Team.Players.GetType())
let test = allValues
|> Seq.map (fun p -> (p.GetValue(docAsync.Team.Players) as ?).Name) // HOW TO GET THE TYPED PLAYER HERE ?
|> fun p -> printfn p

编辑:我尝试使用 GetType 和 System.Convert.ChangeType

EDIT2:这是 JSON 的简化版本:

{
"Team": {
"id": "8",
"players": {
"17878": {
"info": {
"idteam": 8,
"idplayer": 17878,
"note": 6
}
},
"18507": {
"info": {
"idteam": 8,
"idplayer": 18507,
"note": 5
}
}
}
}
}

编辑 3 :

我在 C# 中找到了一个简单的解决方案(感谢 JSON.net 和动态),但出于学习目的,如果有人愿意提供帮助,我想在 F# 中做同样的事情:

        private static List<Player> Parse(string jsonString)
{
dynamic jsonObject = JsonConvert.DeserializeObject(jsonString);

var players = ParseItems(jsonObject.Home.players);

return players;
}

private static List<Player> ParseItems(dynamic items)
{
List<Player> itemList = new List<Player>();
foreach (var item in items)
{
itemList.Add(new Player()
{
idplayer = item.Value.info.idplayer,
lastName = item.Value.info.lastname,
note = item.Value.info.note
});
}
return itemList;
}

最佳答案

可以混合使用 JsonTypeProvider 和解析 Json。例如:

[<Literal>]
let sample = """{
"Team": {
"id": "8",
"players": {
"17878": {
"info": {
"idteam": 8,
"idplayer": 17878,
"note": 6
}
},
"18507": {
"info": {
"idteam": 8,
"idplayer": 18507,
"note": 5
}
}
}
}
}"""

type Player = {IdTeam:int; IdPlayer:int; Note:int}

type Simple = JsonProvider<sample>
let docAsync = Simple.GetSample()

let json = docAsync.Team.Players.JsonValue

let parseInfo (json:JsonValue) =

let id_team = (json.GetProperty "idteam").AsInteger()
let id_player = (json.GetProperty "idplayer").AsInteger()
let note = (json.GetProperty "note").AsInteger()

{IdTeam = id_team; IdPlayer = id_player; Note = note}

let players =
json.Properties()
|> Array.map(fun (_,x) -> x.GetProperty "info")
|> Array.map (parseInfo)

players
|> Array.iter (printfn "%A")

关于c# - 使用类型提供程序和 JSON API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42095960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com