JSON
{
"3G/4G": [
{
"rs": "15",
"desc": "Pack validity Active Plan Total data 1 GB Data at high speed* 1 GB",
"validity": "N/A",
"last_update": "01-01-1970"
}
],
"2G": [
{
"rs": "26",
"desc": "JioPhone Pack validity 28 Days Total data 2 GB Data at high speed* 2 GB Voice NA SMS NA",
"validity": "28 days"
}
]
}here im getting my response like this when i did deserialization im unable to map this list to respected entity class
C#
public class Response
{
public int status { get; set; }
public Rdata rdata { get; set; }
}
public class Rdata
{
[JsonProperty(PropertyName="3G/4G")]
public List _3G4G { get; set; }
[JsonProperty(PropertyName="2G")]
public List _2G { get; set; }
public List Romaing { get; set; }
}
public class _3G4G
{
public decimal rs { get; set; }
public string desc { get; set; }
public string validity { get; set; }
public string last_update { get; set; }
}
public class _2G
{
public decimal rs { get; set; }
public string desc { get; set; }
public string validity { get; set; }
public string last_update { get; set; }
}this is what i tried. I just want to map the response 3G/4G with my class i know we cant keep class name with numbers but im unable to map properties for 2g and 3g/4g Pls help me

Amit MohantyRank #112 · 20K reputationPosted Feb 19, 2024, 8:09 AM EST
Check this
using Newtonsoft.Json; using System.Collections.Generic; public class Response { public int status { get; set; } public Rdata rdata { get; set; } } public class Rdata { [JsonProperty(PropertyName = "3G/4G")] public List ThreeGFourG { get; set; } [JsonProperty(PropertyName = "2G")] public List TwoG { get; set; } public List Romaing { get; set; } } public class _3G4G { public decimal rs { get; set; } public string desc { get; set; } public string validity { get; set; } public string last_update { get; set; } } public class _2G { public decimal rs { get; set; } public string desc { get; set; } public string validity { get; set; } public string last_update { get; set; } } public class Romaing { // Define properties for Romaing class } class Program { static void Main(string[] args) { string json = @" { ""status"": 200, ""rdata"": { ""3G/4G"": [ { ""rs"": ""15"", ""desc"": ""Pack validity Active Plan Total data 1 GB Data at high speed* 1 GB"", ""validity"": ""N/A"", ""last_update"": ""01-01-1970"" } ], ""2G"": [ { ""rs"": ""26"", ""desc"": ""JioPhone Pack validity 28 Days Total data 2 GB Data at high speed* 2 GB Voice NA SMS NA"", ""validity"": ""28 days"" } ] } }"; Response response = JsonConvert.DeserializeObject(json); if (response != null) { foreach (var item in response.rdata.ThreeGFourG) { Console.WriteLine($"3G/4G - rs: {item.rs}, desc: {item.desc}, validity: {item.validity}, last_update: {item.last_update}"); } foreach (var item in response.rdata.TwoG) { Console.WriteLine($"2G - rs: {item.rs}, desc: {item.desc}, validity: {item.validity}, last_update: {item.last_update}"); } } } }Jignesh KumarRank #91 · 22.9K reputationPosted Feb 19, 2024, 6:26 AM EST
Hello Jaya Prakas,
You can use below class to convert your json response to object,
public class ResponseData { [JsonProperty("3G/4G")] public List _3G4G { get; set; } [JsonProperty("2G")] public List _2G { get; set; } } public class _3G4G { public string rs { get; set; } public string desc { get; set; } public string validity { get; set; } public string last_update { get; set; } } public class _2G { public string rs { get; set; } public string desc { get; set; } public string validity { get; set; } }Note : add extra property as per your need in above class.
You need to convert your json object to class,
ResponseData responseData = JsonConvert.DeserializeObject(jsonString); //After this you can assign you final class here Response response = new Response(); response.Status = 1; // Status field should be boolean value, Your status return true value response.rData = responseData ;Jignesh KumarRank #91 · 22.9K reputationPosted Feb 19, 2024, 7:38 AM EST
Hello Jaya Prakas,
I have updated my previous answer, please do below change in your Rdata class and add [JsonProperty("3G/4G")] instead of [JsonProperty(PropertyName="3G/4G")]to your class.
public class ResponseData { [JsonProperty("3G/4G")] public List _3G4G { get; set; } [JsonProperty("2G")] public List _2G { get; set; } }Alpesh ManiyaRank #832 · 2.7K reputationPosted Feb 19, 2024, 9:11 AM EST
using Newtonsoft.Json; using System.Collections.Generic; public class MobilePlan { [JsonProperty("3G/4G")] public List ThreeGFourG { get; set; } [JsonProperty("2G")] public List TwoG { get; set; } } public class PlanDetail { public string rs { get; set; } public string desc { get; set; } public string validity { get; set; } public string last_update { get; set; } }Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 19, 2024, 6:42 AM EST
not working