C# 使用类型提供程序和JSON API
我正在尝试使用一个返回JSON的API,该API构建为: Team.player1/player2/player3。。。(玩家是作为属性而不是阵列构建的) 我想用反射来做,但是很难找到一个球员C# 使用类型提供程序和JSON API,c#,json,f#,type-providers,f#-data,C#,Json,F#,Type Providers,F# Data,我正在尝试使用一个返回JSON的API,该API构建为: Team.player1/player2/player3。。。(玩家是作为属性而不是阵列构建的) 我想用反射来做,但是很难找到一个球员 type Simple = JsonProvider<"sample.json"> let wbReq = "API-FOR-TEAM" let docAsync = Simple.Load(wbReq) let allValues = FSharpType.GetRecordF
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
编辑3:
我在C#中找到了一个简单的解决方案(感谢JSON.net和dynamic),但出于学习目的,如果有人想要帮助,我也希望在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;
}
私有静态列表解析(字符串jsonString)
{
动态jsonObject=JsonConvert.DeserializeObject(jsonString);
var players=ParseItems(jsonObject.Home.players);
返回球员;
}
私有静态列表项(动态项)
{
List itemList=新列表();
foreach(项目中的var项目)
{
添加(新玩家()
{
idplayer=item.Value.info.idplayer,
lastName=item.Value.info.lastName,
备注=item.Value.info.note
});
}
返回项目列表;
}
您可以混合使用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")
[]
让样本为“”{
“团队”:{
“id”:“8”,
“玩家”:{
"17878": {
“信息”:{
“idteam”:8,
“idplayer”:17878,
“注”:6
}
},
"18507": {
“信息”:{
“idteam”:8,
“idplayer”:18507,
“注”:5
}
}
}
}
}"""
键入Player={IdTeam:int;IdPlayer:int;Note:int}
类型Simple=JsonProvider
让docAsync=Simple.GetSample()
让json=docAsync.Team.Players.JsonValue
让parseInfo(json:JsonValue)=
让id_team=(json.GetProperty“idteam”).AsInteger()
让id_player=(json.GetProperty“idplayer”).AsInteger()
let note=(json.GetProperty“note”).AsInteger()
{IdTeam=id\u team;IdPlayer=id\u player;Note=Note}
让玩家=
json.Properties()
|>Array.map(fun(\ux)->x.GetProperty“info”)
|>Array.map(parseInfo)
球员
|>Array.iter(printfn“%A”)
不确定我是否理解您想要获取的内容也许您可以展示json的示例?当然@FoggyFinder让我知道它现在是否更好,thxwell,它对我来说不是有效的json,简单的方法是创建您自己的类型(甚至类型),然后从docAsync
[<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")