Home > OS >  How to deserialize complex json strings?
How to deserialize complex json strings?

Time:07-07

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?

CodePudding user response:

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);

CodePudding user response:

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);
  • Related