Deserialize json based on fields in .Net (C#)

后端 未结 5 616
余生分开走
余生分开走 2021-02-09 12:05

I\'m writing an app that gets a Json list of objects like this:

[
   {
       \"ObjectType\": \"apple\",
       \"ObjectSize\": 35,
       \"ObjectC         


        
5条回答
  •  庸人自扰
    2021-02-09 12:25

    This is not an answer but in C# 6.0 you will be able to do this:

      using Microsoft.VisualStudio.TestTools.UnitTesting;
      using Newtonsoft.Json.Linq;
      [TestMethod]
      public void JsonWithDollarOperatorStringIndexers()
      {
    
          // Additional data types eliminated for elucidation
          string jsonText = @"
            {
              'Byte':  {
                'Keyword':  'byte',
                'DotNetClassName':  'Byte',
                'Description':  'Unsigned integer',
                'Width':  '8',
                'Range':  '0 to 255'
                        },
              'Boolean':  {
                'Keyword':  'bool',
                'DotNetClassName':  'Boolean',
                'Description':  'Logical Boolean type',
                'Width':  '8',
                'Range':  'True or false.'
                          },
            }";
          JObject jObject = JObject.Parse(jsonText);
          Assert.AreEqual("bool", jObject.$Boolean.$Keyword);
        }
    

    Taken from here.

提交回复
热议问题