Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The json files are like the text below.

{
  "lmod_file": {
    "crlmod": true, 
    "name": "corn silage;Ffcult with disked radish cover, z4", 
    "params": {
      "param": [
        {
          "data": [
            {
              "file_key": null
            }, 
            {
              "file_key": "999", 
              "value": "vegetations\\Corn, silage"
            }, 
            {
              "file_key": null
            }, 
            {
              "file_key": null
            }, 
            {
              "file_key": "999", 
              "value": "vegetations\\Brassica, cover, forage, release"
            }
          ], 
          "name": "VEG_PTR"
        }, 
        {
          "data": [
            {
              "file_key": "999", 
              "value": "operations\\Cultivator, field 6-12 in sweeps"
            }, 
            {
              "file_key": "999", 
              "value": "operations\\Planter, double disk opnr, 40 in rows"
            }, 
            {
              "file_key": "999", 
              "value": "operations\\Harvest, silage"
            }, 
            {
              "file_key": "999", 
              "value": "operations\\Disk, tandem light finishing"
            }, 
            {
              "file_key": "999", 
              "value": "operations\\Drill or air seeder, double disk"
            }
          ], 
          "name": "OP_PTR"
        }, 
        {
          "data": [
            "0001-04-28", 
            "0001-05-01", 
            "0001-09-15", 
            "0001-09-16", 
            "0001-09-17"
          ], 
          "name": "OP_DATE"
        }, 
        {
          "data": [
            "NULL", 
            "23", 
            "NULL", 
            "NULL", 
            "1500"
          ], 
          "name": "MAN_OP_VEG_NUM_HARV_UNITS"
        }, 
        {
          "data": [
            {
              "file_key": null
            }, 
            {
              "file_key": null
            }, 
            {
              "file_key": null
            }, 
            {
              "file_key": null
            }, 
            {
              "file_key": null
            }
          ], 
          "name": "EXT_RES_PTR"
        }, 
        {
          "data": [
            "NULL", 
            "NULL", 
            "NULL", 
            "NULL", 
            "NULL"
          ], 
          "name": "RES_ADDED"
        }, 
        {
          "data": true, 
          "name": "WEPP_NAMES", 
          "type": "Bool", 
          "value": "true"
        }
      ]
    }, 
    "path": "managements\\CMZ 04\\a.Single Year/Single Crop Templates\\Corn silage"
  }
}


What I have tried:

Models.JsonLmodFileClass.LmodFileClass lmodObj = JsonConvert.DeserializeObject<Models.JsonLmodFileClass.LmodFileClass>(text);


classes defined below:
public class JsonLmodFileClass
{
    public class LmodFileClass
    {

        public class Rootobject
        {
            public Lmod_File lmod_file { get; set; }
            public Rootobject()
            {
                lmod_file = new Lmod_File();
            }
        }

        public class Lmod_File
        {
            public bool crlmod { get; set; }
            public string name { get; set; }
            public Params _params { get; set; }
            public string path { get; set; }
            public Lmod_File()
            {
                _params = new Params();
            }
        }

        public class Params
        {
            public Param[] param { get; set; }

            public Params()
            {
                param = new Param[1];
            }
        }

        public class Param1
        {
            public object data { get; set; }
            public string name { get; set; }
            public string type { get; set; }
            public string value { get; set; }
        }
        public class TJsonArrayOfStr
        {
            public string name { get; set; }
            public List<string> values { get; set; }
            public TJsonArrayOfStr()
            {
                values = new List<string>();
            }
        }
        public class TJsonArrayOfKeyValues
        {
            public string name { get; set; }
            public List<TJsonFileKeyValue> values { get; set; }
            public TJsonArrayOfKeyValues()
            {
                values = new List<TJsonFileKeyValue>();
            }
        }
        public class Param
        {
            public TJsonArrayOfKeyValues vegPtr;            //Array file_key - value
            public TJsonArrayOfKeyValues opPtr;             //Array file_key - value
            public TJsonArrayOfStr       opDate;            //Array of string values
            public TJsonArrayOfStr       manOpVegHarvest;   //Array of string values
            public TJsonArrayOfKeyValues exRes;             //Array file_key - value
            public TJsonArrayOfStr       resAdded;          //Array of string values
            public Param()
            {
                vegPtr = new TJsonArrayOfKeyValues();
                vegPtr.name = "VEG_PTR";

                opPtr       = new TJsonArrayOfKeyValues();
                opPtr.name = "OP_PTR";

                opDate = new TJsonArrayOfStr();
                opDate.name = "OP_DATE";

                manOpVegHarvest = new TJsonArrayOfStr();
                manOpVegHarvest.name = "MAN_OP_VEG_NUM_HARV_UNITS";

                exRes       = new TJsonArrayOfKeyValues();
                exRes.name = "EXT_RES_PTR";

                resAdded = new TJsonArrayOfStr();
                resAdded.name = "RES_ADDED";
            }
        }
    }
}
Posted
Updated 23-Oct-20 4:01am

You've not explained what the error is exactly but you're trying to deserialize to a LmodFileClass but that class doesn't have the "lmod_file" property that's expected. Instead you've created the inner Rootobject class containing this, have you tried moving lmod_file to the parent LmodFileClass?

C#
public class LmodFileClass
{
   public Lmod_File lmod_file { get; set; }

   ...
}
 
Share this answer
 
For the Json shared by you, following is the C# model class:
C#
public class Param
{
    public object data { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public string value { get; set; }
}

public class Params
{
    public IList<Param> param { get; set; }
}

public class LmodFile
{
    public bool crlmod { get; set; }
    public string name { get; set; }
    public Params params { get; set; }
    public string path { get; set; }
}

public class ExampleRoot
{
    public LmodFile lmod_file { get; set; }
}
Usage:
C#
// ExampleRoot myDeserializedClass = JsonConvert.DeserializeObject<ExampleRoot>(myJsonResponse);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900