2016-02-03 18 views
5

Bir HTTP web isteğinin içeriğinden bir nesneye seri hale getirdiğim bir F # türüm var. Aradığım API, bir oda protokolü kullanıyor ve bu isteğin içeriği, @odata.context anahtarını içeren aşağıdaki formata sahip.Bir @ # sembolünü kullanarak json.net ve bir json özelliği kullanarak bir @ serileştirme

type Success = { 
    [<JsonProperty(PropertyName = "@odata.context")>] 
    ``odata.context``: string; 
    Value: string; } 

odata.context hep bu durumda null olduğu şöyle Geri benim F # tipine içerik serisini Json.NET kullanıyorum

{ 
    "@odata.context":"OData", 
    "Value":"token" 
} 

, F # türüdür.

ben hem çalıştılar (F # tipi mülkiyet adına @ sembolüyle) ve sonuç

let test1 = JsonConvert.DeserializeObject<Success>("{\"@odata.context\": \"odata.context\", \"Value\": \"token\"}")) 

(F # tipi mülkiyet adına @ sembolüyle olmadan) Bu doğru serileştirilemezse alır NULL aşağıdaki .

let test2 = JsonConvert.DeserializeObject<Success>("{\"odata.context\": \"odata.context\", \"Value\": \"token\"}")) 

Bunun, özellik adındaki @ sembolüyle ilgili olabileceğini düşünüyorum.

Çözüm hakkında herhangi bir fikir harika olurdu.

+1

kullandığınız sürümü nedir? https://dotnetfiddle.net/s6W7yJ –

+0

F # 3.0 ve json.net 6.0.4 kullanıyorum –

+1

json.net bir. Net sürümü olduğundan ve C# ile çalışacağından SO burada C# yanıtlarına baktınız mı? –

cevap

2

Json.Net ürününü daha yeni bir sürüme (örneğin, 8.0.2) güncelleştirme fırsatınız yoksa, Newtonsoft.Json.Linq kullanabilirsiniz.

Örnek:

open System 
open Newtonsoft.Json.Linq 

type Success = { 
    ``odata.context``: string; 
    Value: string; } 

let json = "{\"@odata.context\":\"OData\",\"Value\":\"token\"}" 

let p = JObject.Parse(json) 

{``odata.context`` = p.["@odata.context"] |> string ;Value = p.["Value"] |> string} 
|> printfn "%A" 

Baskı:

{odata.context = "OData"; 
Value = "token";} 

Bağlantı:

https://dotnetfiddle.net/SR16Ci

İlgili konular