dlimes
dlimes

Reputation: 105

Deserialize Nested JSON into classes/objects, in turn - serialize it back into string

Here's my goal, I want to retrieve JSON data from a source, deserialize into objects, filter out objects based on value, and finally serialize it back into JSON string format. Here's (an example) what I have for JSON data:

[  
   {  
      "property":"prop1",
      "status" : {
         status1: "A",
         status2: -1,
         status3: "Success", 
      },
      "offlist":[  
         {  
            "description":"description blah",
            "type":"F",
            "values":{  
               "value1":30.0,
               "value2":0.0
            }
         },
         {  
            "description":"description blah",
            "type":"F",
            "values":{  
               "value1":30.0,
               "value2":0.0
            }
         }
      ]
   },
   {  
      "property":"prop2",
      "status" : {
         status1: "A",
         status2: -1,
         status3: "Success", 
      },
      "offlist":[  
         {  
            "description":"description blah",
            "type":"Q",
            "values":{  
               "value1":30.0,
               "value2":0.0
            }
         }
      ]
   }
]

Here's my classes:

public class offerModel {
    public List<offlist> offlist { get; set; }
    public status statuses { get; set; }
    public string property{ get; set; }
}
public class offlist{
    public string description{ get; set; }
    public string type{ get; set; }
    public values values { get; set; }        
}
public class values{
    public double value1 { get; set; }
    public double value2{ get; set; }
}

public class statuses {
    public string status1 { get; set; }
    public double status2 { get; set; }
    public string status3 { get; set; }

}
public class RootObj {
    public List<offerModel> offModels { get; set; }
}

Upon attempt of deserializing into objects, I get a List of 5 objects back (Which is intended), but all those objects are null. See Below:

var obj = JsonConvert.DeserializeObject<List<RootObj>>(jsonstring);

I then want to remove any 'offlist' objects with the 'type' value equaling 'F'.

After removing those, I then want to put back into JSON string format, which I believe would be very similar to below, I'm just not able to get it to deserialize in the correct fashion.

var json = JsonConvert.SerializeObject(obj);

Let me know if I left any details out...

Upvotes: 0

Views: 951

Answers (2)

Atul Patel
Atul Patel

Reputation: 81

Deserialize nested JSON into Class. not on dictionary based but it's useful.

Step 01: open the link https://jsonformatter.org/json-parser

Step 02: copy your json file or contents.

Step 03: Open above link. copy contents and past in to left side and click on to JSON Parser button.

Step 04: Click on download button. Downloading the jsonformatter.txt file. Successfully download the file as jsonformatter.txt.

Step 05: Copy step 02 content and open url https://json2csharp.com/.Copy contents and past in to left side and click on to Convert button.

Step 06: In Scripting.

(A) Create myRootClass.cs file and copy and past down contents to your file.[[System.Serializable] it's used in unity 3d software c# scripting]

[System.Serializable]
public class myRootClass
{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
   [System.Serializable]
    public class Status
   {
    public string status1 { get; set; }
    public int status2 { get; set; }
    public string status3 { get; set; }
   }
   [System.Serializable]
   public class Values
   {
    public double value1 { get; set; }
    public double value2 { get; set; }
   }
   [System.Serializable]
   public class Offlist
   {
    public string description { get; set; }
    public string type { get; set; }
    public Values values { get; set; }
   }

   public class Root
  {
    public string property { get; set; }
    public Status status { get; set; }
    public List<Offlist> offlist { get; set; }
   }
}

    

(B) Read the jsonformatter.txt file.

// Read entire text file content in one string 
    string  textFilePath = "C:/Users/XXX/Downloads/jsonformatter.txt";//change path
    string jsontext = System.IO.File.ReadAllText(textFilePath);  
    Debug.Log("Read Json"+jsontext);// used Console.Writeline

(C) Convert string into C# and show the data.

  Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(jsontext); 
  
  Debug.Log("myDeserializedClass.property:- "+myDeserializedClass.property); //used Console.Writeline
 var offlists= myDeserializedClass.offlist; // used foreach and view data

Upvotes: 1

ASpirin
ASpirin

Reputation: 3651

Change serialization to JsonConvert.DeserializeObject<List<offerModel>>(data) and fix offerModel class property statuses to public statuses status { get; set; } to make your sample deserializable If you want to keep a root object you can do next:

public class RootObj : List<offerModel>{}
JsonConvert.DeserializeObject<RootObj>(data)

Upvotes: 0

Related Questions