Can I deserialize json to anonymous type in c#?

前端 未结 7 691
半阙折子戏
半阙折子戏 2021-02-04 07:13

I read from the DB a long json. I want just one attribute of that json.

I have got two options: a. Create an interface for that json and deserialize to that interface.

7条回答
  •  生来不讨喜
    2021-02-04 07:28

    On .NET 4:

    You can do something kind of like what you want minus the need for regex (and you shouldn't use regex for something like this!) by using the dynamic feature of C# 4.0 described here: http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx

    The only downside is that you can't guarantee what the exact structure of the object is.

    The upswing is that instead of accessing members via yourDynamicObject['blah'], it's the more duck-type-ish yourDynamicObject.blah

    On .NET 3.5:

    You can use Json.NET: http://json.codeplex.com/

提交回复
热议问题