简体   繁体   English

如何使用 F# 访问 ExpandoObject 属性?

[英]How to access ExpandoObject properties using F#?

I am using the Flurl library to call a webservice, which returns a JSON我正在使用 Flurl 库调用 web 服务,它返回一个 JSON

{"data":{"charges":[{"code":30200757,"reference":"","dueDate":"18/12/2018","checkoutUrl":"https://sandbox.boletobancario.com/boletofacil/checkout/C238E9C42A372D25FDE214AE3CF4CB80FD37E71040CBCF50","link":"https://sandbox.boletobancario.com/boletofacil/charge/boleto.pdf?token=366800:m:3ea89b5c6579ec18fcd8ad37f07d178f66d0b0eb45d5e67b884894a8422f23c2","installmentLink":"https://sandbox.boletobancario.com/boletofacil/charge/boleto.pdf?token=30200757:10829e9ba07ea6262c2a2824b36c62e7c5782a43c855a1004071d653dee39af0","payNumber":"BOLETO TESTE - Não é válido para pagamento","billetDetails":{"bankAccount":"0655/46480-8","ourNumber":"176/30200757-1","barcodeNumber":"34192774200000123001763020075710655464808000","portfolio":"176"}}]},"success":true}

This is my F# code:这是我的 F# 代码:

let c = "https://sandbox.boletobancario.com/boletofacil/integration/api/v1/"
        .AppendPathSegment("issue-charge")
        .SetQueryParams(map)
        .GetJsonAsync()

c.Wait()
let j = c.Result
let success = j?success

I checked and the variable j contains an obj ("System.Dynamic.ExpandoObject")我检查了变量j包含一个 obj ("System.Dynamic.ExpandoObject")

How can I access, for example, the success value of this obj in variable j?例如,如何访问变量 j 中此 obj 的成功值? And how to access the data ?以及如何访问数据

Visual Studio 2019 截图

I don't have experience with that particular library, but if the result is just a normal ExpandoObject , then the following should do the trick.我没有使用那个特定库的经验,但如果结果只是一个普通的ExpandoObject ,那么以下应该可以解决问题。

First of all, ExpandoObject implements the IDictionary<string, obj> , so you can cast the value to IDictionary and then add or get members as you wish:首先, ExpandoObject实现了IDictionary<string, obj> ,因此您可以将值转换为IDictionary ,然后根据需要添加或获取成员:

open System.Dynamic
open System.Collections.Generic

let exp = ExpandoObject() 

// Adding and getting properties using a dictionary    
let d = exp :> IDictionary<string, obj>
d.Add("hi", 123)
d.["hi"]

If you want to use the ?如果要使用? syntax, you can define the ?语法,您可以定义? operator yourself, doing exactly the same as above:自己操作,和上面的完全一样:

let (?) (exp:ExpandoObject) s = 
  let d = exp :> IDictionary<string, obj>
  d.[s]

exp?hi

That said, if you can use type providers, it'd be a lot easier to do this with F# Data for the JSON parsing, because then you could just replace all the dynamic unsafe ?也就是说,如果您可以使用类型提供程序,那么使用F# Data进行 JSON 解析会容易得多,因为这样您就可以替换所有动态 unsafe ? accesses with type-checked ones!使用类型检查的访问!

You can use a predefined ?您可以使用预定义的? operator for all your DynamicObject interop needs with fsprojects/FSharp.Interop.Dynamic使用fsprojects/FSharp.Interop.Dynamic满足您所有 DynamicObject 互操作需求的运算符

open FSharp.Interop.Dynamic
let ex1 = ExpandoObject()
ex1?Test<-"Hi"//Set Dynamic Property
ex1?Test //Get Dynamic

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

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