convert json to c# class with variable structure

sil bahi 96 Reputation points
2021-06-23T08:33:43.993+00:00

Hello,

I'm working in asp.net mvc.
I have a method action in myControllerwhich takes arguments as parameters, I enter an input file and I call an exe. I receive as a return an output file in json format.

I would like to manipulate the existing informations of this file by desiarilization but I cannot recover some data because of the structure of the json file obtained.

here is an example of json obtained:

{
"messageErreur" : "",
"hashProduction" : {
"detailsContenants" : {
"000031" : {
"nbPlis" : 26,
"poids" : 1180,
"CP-Min" : "66130",
"CP-Max" : "67140",
"typeContenant" : "TF"
},
"000011" : {
"nbPlis" : 26,
"poids" : 1200,
"CP-Min" : "11000",
"CP-Max" : "11400",
"typeContenant" : "TF"
},
"000049" : {
"nbPlis" : 27,
"poids" : 1200,
"CP-Min" : "81100",
"CP-Max" : "82240",
"typeContenant" : "TF"
},
"000026" : {
"nbPlis" : 25,
"poids" : 1180,
"CP-Min" : "57100",
"CP-Max" : "57560",
"typeContenant" : "TF"
}
},
"nbPlisContenant" : 3,
"identifiant_lotissement_tf" : "000864",
"code_produit" : "G2",
"identifiant_lotissement_dpt" : "000863"
},
"processDurationSecondes" : 3,
"processDuration" : " 2 wallclock secs ( 1.58 usr + 0.14 sys = 1.72 CPU)"
}

How can I convert it to a C # class knowing that the 1st part generated in the json file changes between one treatment and the other? (000031, 000011, 000049, 000026 : these value are variables so to access this file and use ("nbPlis", "poids" , "CP-Min", "CP-Max", "typeContenant") to display it I couldn't find the way to do that

public class _000031
{
public int nbPlis { get; set; }
public int poids { get; set; }
[JsonProperty("CP-Min")]
public string CPMin { get; set; }
[JsonProperty("CP-Max")]
public string CPMax { get; set; }
public string typeContenant { get; set; }
}
public class _000011
{
public int nbPlis { get; set; }
public int poids { get; set; }
[JsonProperty("CP-Min")]
public string CPMin { get; set; }
[JsonProperty("CP-Max")]
public string CPMax { get; set; }
public string typeContenant { get; set; }
}
public class _000049
{
public int nbPlis { get; set; }
public int poids { get; set; }
[JsonProperty("CP-Min")]
public string CPMin { get; set; }
[JsonProperty("CP-Max")]
public string CPMax { get; set; }
public string typeContenant { get; set; }
}
public class _000026
{
public int nbPlis { get; set; }
public int poids { get; set; }
[JsonProperty("CP-Min")]
public string CPMin { get; set; }
[JsonProperty("CP-Max")]
public string CPMax { get; set; }
public string typeContenant { get; set; }
}
public class DetailsContenants
{
public _000031 _000031 { get; set; }
public _000011 _000011 { get; set; }
public _000049 _000049 { get; set; }
public _000026 _000026 { get; set; }
}

public class HashProduction
{
    public DetailsContenants detailsContenants { get; set; }
    public int nbPlisContenant { get; set; }
    public string identifiant_lotissement_tf { get; set; }
    public string code_produit { get; set; }
    public string identifiant_lotissement_dpt { get; set; }
}

public class Root
{
    public string messageErreur { get; set; }
    public HashProduction hashProduction { get; set; }
    public int processDurationSecondes { get; set; }
    public string processDuration { get; set; }
}

How can I changer the first part? any suggestions or helps please? Thnak you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,415 questions
0 comments No comments
{count} votes

Accepted answer
  1. sil bahi 96 Reputation points
    2021-06-23T11:33:41.26+00:00

    In HashProduction class I changed the first line to :

    public Dictionary<string, Details> detailsContenants { get; set; }

    and adding this:
    public class Details
    {
    public int nbPlis { get; set; }
    public int poids { get; set; }
    [JsonProperty("CP-Min")]
    public string CPMin { get; set; }
    [JsonProperty("CP-Max")]
    public string CPMax { get; set; }
    public string typeContenant { get; set; }
    }
    and now I can get the conversion correctly and browse into my object.

    0 comments No comments

0 additional answers

Sort by: Most helpful