-1

1.My json string is like this following:

{
  "TagOptions": {
    "AResouceGroupId": "rg-aekzsbuw5climmq",
    "BResouceGroupId": "rg-aekztehix5f6eei",
    "CResouceGroupId": "rg-aek2t7m7dcjqvhq",
    "AClusterGroups": [ 0 ],
    "BClusterGroups": [ 0 ],
    "CClusterGroups": [ 46 ],
    "DClusterGroups": [ 153, 98, 23, 3, 4, 5, 8, 9, 10, 18, 28, 99, 101, 102, 104, 105, 201 ],
    "DataDeleiveryTeamProject": [
      {
        "GroupId": 14,
        "ProjectName": "XXX-A"
      },
      {
        "GroupId": 15,
        "ProjectName": "XXX-B"
      },
      {
        "GroupId": 46,
        "ProjectName": "XXX-C"
      },
      {
        "GroupId": 101,
        "ProjectName": "XXX-D"
      }
    ],
    "DataCenterTeamProject": [
      {
        "GroupId": 0,
        "ProjectName": "Empty"
      }
    ],
    "BazhuayuTeamProject": [
      {
        "GroupId": 153,
        "ProjectName": "XXX-OP1"
      },
      {
        "GroupId": 98,
        "ProjectName": "XXX-OP2"
      },
      {
        "GroupId": 23,
        "ProjectName": "XXX-OP3"
      },
      {
        "GroupId": 201,
        "ProjectName": "XXX-OP4"
      },
      {
        "GroupId": 3,
        "ProjectName": "XXX-OP5"
      },
      {
        "GroupId": 4,
        "ProjectName": "XXX-OP6"
      },
      {
        "GroupId": 8,
        "ProjectName": "XXX-OP7"
      },
      {
        "GroupId": 9,
        "ProjectName": "XXX-OP8"
      },
      {
        "GroupId": 10,
        "ProjectName": "XXX-OP9"
      },
      {
        "GroupId": 28,
        "ProjectName": "XXX-OP10"
      },
      {
        "GroupId": 102,
        "ProjectName": "XXX-OP11"
      },
      {
        "GroupId": 103,
        "ProjectName": "XXX-OP12"
      },
      {
        "GroupId": 104,
        "ProjectName": "XXX-OP13"
      },
      {
        "GroupId": 105,
        "ProjectName": "XXX-OP14"
      },
      {
        "GroupId": 18,
        "ProjectName": "XXX-OP15"
      },
      {
        "GroupId": 99,
        "ProjectName": "XXX-OP16"
      }
    ]
  }
}

2.Then,I try to deserialize the json string to a targete class, the target class is following:

using System.Collections.Generic;

namespace Tag
{
    public class TagOptions
    {
        public string AResouceGroupId { get; set; }

        public string BResouceGroupId { get; set; }

        public string CResouceGroupId { get; set; }

        public int[] AClusterGroups { get; set; }

        public int[] BClusterGroups { get; set; }

        public int[] CClusterGroups { get; set; }

        public int[] OctoparseClusterGroups { get; set; }

        public List<Project> ATeamProject { get; set; }

        public List<Project> BTeamProject { get; set; }

        public List<Project> CTeamProject { get; set; }
    }

    public class Project
    {
        public int GroupId { get; set; }

        public string ProjectName { get; set; }
    }
}

the method is like this, I use .netframework4.5 and Newtonsoft.Json

var s = jsonSerializer.Deserialize<CloudNodeTagOptions>(jsonStr);

but the result is null. Is there are some solution to solve the problem?

Orace
  • 7,822
  • 30
  • 45
Boi Fox
  • 45
  • 5
  • 1
    Use https://json2csharp.com/ to create C# classes for the JSON – Chetan Jul 06 '22 at 09:30
  • see this MS [docs](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0#how-to-read-json-as-net-objects-deserialize) page for example to deserialize to your model class. – Anand Sowmithiran Jul 06 '22 at 09:31
  • 3
    Show us `CloudNodeTagOptions`. Also `DataDeleiveryTeamProject` and other `xxxTeamProject` entry in the json doesn't have a matching property in the `TagOptions` class. – Orace Jul 06 '22 at 09:34
  • Your answer is helpful. Got it. and the tool is easy to use. Many thanks to you. – Boi Fox Jul 07 '22 at 03:56

2 Answers2

2

Your model is not in compliance with the JSON that you are trying to work with. Change your model to the sample shown below, then the Deserialize would work.


public class MyModel
{
    public Tagoptions TagOptions { get; set; }
}

public class Tagoptions
{
    public string AResouceGroupId { get; set; }
    public string BResouceGroupId { get; set; }
    public string CResouceGroupId { get; set; }
    public int[] AClusterGroups { get; set; }
    public int[] BClusterGroups { get; set; }
    public int[] CClusterGroups { get; set; }
    public int[] DClusterGroups { get; set; }
    public Project[] DataDeleiveryTeamProject { get; set; }
    public Project[] DataCenterTeamProject { get; set; }
    public Project[] BazhuayuTeamProject { get; set; }
}

public class Project
{
    public int GroupId { get; set; }
    public string ProjectName { get; set; }
}

Deserialize as below,

   var s = jsonSerializer.Deserialize<MyModel>(jsonStr);
Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
0

There's target object definition is not correct, JSON properties must match class properties and types. Use the following classes:

public class TargetClass
{
    public TagOptions TagOptions { get; set; }
}

public class TagOptions
{
    public string AResouceGroupId { get; set; }

    public string BResouceGroupId { get; set; }

    public string CResouceGroupId { get; set; }

    public int[] AClusterGroups { get; set; }

    public int[] BClusterGroups { get; set; }

    [JsonProperty("DClusterGroups")]
    public int[] OctoparseClusterGroups { get; set; }

    [JsonProperty("DataDeleiveryTeamProject")]
    public List<Project> ATeamProject { get; set; }

    [JsonProperty("DataCenterTeamProject")]
    public List<Project> BTeamProject { get; set; }

    [JsonProperty("BazhuayuTeamProject")]
    public List<Project> CTeamProject { get; set; }
}

public class Project
{
    public int GroupId { get; set; }

    public string ProjectName { get; set; }
}

Note that I've added JsonProperty attribute to tell deserializer the correct property name.

Then you can use the following code to deserialize specifying the target class:

var jsonObj = JsonConvert.DeserializeObject<TargetClass>(jsonStr);
ale91
  • 316
  • 3
  • 8