Deserializing json to anonymous object in c#

前端 未结 5 1538
深忆病人
深忆病人 2020-12-11 00:56

How do I convert a string of json formatted data into an anonymous object?

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 01:20

    vb.net using Newtonsoft.Json :

    dim jsonstring = "..."
    dim foo As JObject = JObject.Parse(jsonstring)
    dim value1 As JToken = foo("key")
    
    
    e.g.:
    dim jsonstring = "{"MESSAGE":{"SIZE":"123","TYP":"Text"}}"
    dim foo = JObject.Parse(jsonstring)
    dim messagesize As String = foo("MESSAGE")("SIZE").ToString()
    'now in messagesize is stored 123 as String
    

    So you don't need a fixed structure, but you need to know what you can find there.

    But if you don't even know what is inside, than you can enumerate thru that JObject with the navigation members e.g. .first(), .next() E.g.: So you could implement a classical depth-first search and screening the JObject

    (for converting vb.net to c#: http://converter.telerik.com/)

提交回复
热议问题