【问题标题】:Json parsing F#Json 解析 F#
【发布时间】:2011-04-03 03:03:24
【问题描述】:

r@".NETFramework\v4.0\Profile\Client\System.Runtime.Serialization.dll"

open System.Runtime.Serialization
open System.Runtime.Serialization.Json

[<DataContract>]
    type geo = {
        [<field: DataMember(Name = "type")>]
        t:string
        [<field: DataMember(Name = "coordinates")>]
        coordinates:string
        }


let decode (s:string)  = 
    let json = new DataContractJsonSerializer(typeof<geo>)
    let byteArray = Encoding.UTF8.GetBytes(s)
    let stream = new MemoryStream(byteArray)
    json.ReadObject(stream) :?> geo

let tw = {"type":"Point","coordinates":[-7.002648,110.449961]}

decode tw 

这返回 -> 应来自命名空间“”的结束元素“坐标”。从命名空间“”中找到元素“项目”

如何定义 DataMember 坐标以便它理解?

非常感谢

【问题讨论】:

    标签: json f# datacontract


    【解决方案1】:

    参考 System.Runtime.Serialization 和 System.Xml

    (交互:#r "System.Runtime.Serialization")

    open System.IO
    open System.Runtime.Serialization.Json
    open System.Xml
    open System.Text
    
    /// Object to Json 
    let internal json<'t> (myObj:'t) =   
            use ms = new MemoryStream() 
            (new DataContractJsonSerializer(typeof<'t>)).WriteObject(ms, myObj) 
            Encoding.Default.GetString(ms.ToArray()) 
    
    
    /// Object from Json 
    let internal unjson<'t> (jsonString:string)  : 't =  
            use ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(jsonString)) 
            let obj = (new DataContractJsonSerializer(typeof<'t>)).ReadObject(ms) 
            obj :?> 't
    

    【讨论】:

      【解决方案2】:

      这对我有用

      #r "System.Runtime.Serialization"
      
      open System.IO
      open System.Text
      open System.Runtime.Serialization
      open System.Runtime.Serialization.Json
      
      [<DataContract>]
          type geo = {
              [<field: DataMember(Name = "type")>]
              t:string
              [<field: DataMember(Name = "coordinates")>]
              coordinates:float[]
              }
      
      
      let decode (s:string)  = 
          let json = new DataContractJsonSerializer(typeof<geo>)
          let byteArray = Encoding.UTF8.GetBytes(s)
          let stream = new MemoryStream(byteArray)
          json.ReadObject(stream) :?> geo
      
      let tw = "{
          \"type\":\"Point\",
          \"coordinates\":[-7.002648,110.449961]
          }"
      
      let v = decode tw // val v : geo = {t = "Point"; coordinates = [|-7.002648; 110.449961|];}
      

      【讨论】:

      • 感谢您的回复,但字符串中没有要解码的 \",所以我需要找到一种方法让它在没有它们的情况下工作(除 tw.Replace("[", @"\"[").Replace("]",@"]\""),谢谢!
      • [-7.002648,110.449961] 不是字符串值,而是浮点数组,如果您修复地理定义,那么坐标字段是 float[] - 它应该可以解决这个问题。我已经更正了我的示例以证明这一点
      • @Ronald Wildenberg 接受!谢谢
      • 关于如何在不患眼癌的情况下编写tw 定义的小注解:在字符串文字周围使用""",您可以删除所有 `\` 内容并使用普通的旧 JSON。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 2010-11-30
      相关资源
      最近更新 更多