Json.NET deserializing contents of a JObject?

家住魔仙堡 提交于 2020-04-13 20:40:48

问题


If I have a JObject, which contains the property "Fields". How do I pull out the contents of this property to an object[] with deserialized elements?

It seems like no matter what I do, I only get arrays of other JObjects.

myJObject["Fields"] {
  "$type": "System.Object[], mscorlib",
  "$values": [
    123,
    "hello"
  ]
}

In this case, I want to get an object array containing a long 123 and the string "hello".


回答1:


Use ToObject():

        var array = myJObject["Fields"].ToObject<object[]>();

        Debug.Assert(array[0].Equals(123L)); // No assert
        Debug.Assert(array[1].Equals("hello"));  // No assert


来源:https://stackoverflow.com/questions/29531917/json-net-deserializing-contents-of-a-jobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!