Reputation: 63
Can anyone help to write Newtonsoft JSON Deserializer for my JSON format.
I have following json format.
{
"success": true,
"message": "Missing ajax operation. Please contact administrator.",
"data": {
"mode": "new",
"data": {
"1": {
"CustomerCode": "CUST00001",
"Name": "Dell Asia Pacific Sdn. Bhd.",
"Add1": "Plot 76 Mukim 11, Bukit Tengah Industrial Park; Bukit Mertajam; Pulau Pinang; 14000",
"Add2": "",
"Add3": "",
"Daddr1": "Level 21, Suite 21.01, The Gardens South Tower, Mid Valley City, Lingkaran Syed Putra, ; Kuala Lumpur; Wilayah Persekutuan; 59200",
"Daddr2": "",
"Daddr3": "",
"Attn": "",
"Phone": "",
"PhoneA": "",
"Fax": "",
"Area": "",
"Agent": "",
"Email": "",
"CurrCode": "MYR",
"Business": "",
"Term": "30 Days"
},
"2": {
"CustomerCode": "CUST00002",
"Name": "Intel Technology Sdn Bhd",
"Add1": "Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa; Bayan Lepas; Pulau Pinang; 11900",
"Add2": "",
"Add3": "",
"Daddr1": "1st Flr,Standard Chartered Bank Chambers,Lebuh Pantai;Penang; Kuala Lumpur; Wilayah Persekutuan; 10300",
"Daddr2": "",
"Daddr3": "",
"Attn": "",
"Phone": "",
"PhoneA": "",
"Fax": "",
"Area": "",
"Agent": "",
"Email": "",
"CurrCode": "MYR",
"Business": "",
"Term": "30 Days"
},
"3": {
"CustomerCode": "CUST00003",
"Name": "Petronas Carigali Sdn. Bhd.",
"Add1": "Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,; Kuala Lumpur; Wilayah Persekutuan; 50088",
"Add2": "",
"Add3": "",
"Daddr1": "Tower 1,Petronas Twin Towers,K.L.City Centre; Kuala Lumpur; Wilayah Persekutuan; 50088",
"Daddr2": "",
"Daddr3": "",
"Attn": "",
"Phone": "",
"PhoneA": "",
"Fax": "",
"Area": "",
"Agent": "",
"Email": "",
"CurrCode": "MYR",
"Business": "",
"Term": "30 Days"
}
}
}
}
and I have following Class structure
public class CustomerInfo
{
public string CustomerCode { get; set; }
public string Name { get; set; }
public string Add1 { get; set; }
public string Add2 { get; set; }
public string Add3 { get; set; }
public string Daddr1 { get; set; }
public string Daddr2 { get; set; }
public string Daddr3 { get; set; }
public string Attn { get; set; }
public string Phone { get; set; }
public string PhoneA { get; set; }
public string Fax { get; set; }
public string Area { get; set; }
public string Agent { get; set; }
public string Email { get; set; }
public string CurrCode { get; set; }
public string Business { get; set; }
public string Term { get; set; }
}
public class CustomerData
{
public CustomerInfo customerInfo { get; set; }
}
public class Data
{
public string mode { get; set; }
public CustomerData custdata { get; set; }
}
public class CustomerRootObject
{
public bool success { get; set; }
public string message { get; set; }
public Data data { get; set; }
}
}
I have difficulties to parse this json format I wrote following but it is not working any help much appreciated.
List<CustomerRootObject> customerlist = JsonConvert.DeserializeObject<List<CustomerRootObject>> (json);
foreach (var info in customerlist) {Console.WriteLine(info.data.custdata.CustomerCode);}
Upvotes: 1
Views: 221
Reputation: 129687
It doesn't work because your class structure doesn't match the JSON.
Try changing your Data
class to this:
public class Data
{
public string mode { get; set; }
[JsonProperty("data")]
public Dictionary<int, CustomerInfo> customers { get; set; }
}
Then, deserialize like this:
CustomerRootObject obj = JsonConvert.DeserializeObject<CustomerRootObject>(json);
You can list out the customers like this:
foreach (KeyValuePair<int, CustomerInfo> kvp in obj.data.customers)
{
Console.WriteLine("----- Customer " + kvp.Key + " -----");
Console.WriteLine("CustomerCode: " + kvp.Value.CustomerCode);
Console.WriteLine("Name: " + kvp.Value.Name);
Console.WriteLine("Add1: " + kvp.Value.Add1.Replace(";", "\r\n"));
}
Full demo:
class Program
{
static void Main(string[] args)
{
string json = @"
{
""success"": true,
""message"": ""Missing ajax operation. Please contact administrator."",
""data"": {
""mode"": ""new"",
""data"": {
""1"": {
""CustomerCode"": ""CUST00001"",
""Name"": ""Dell Asia Pacific Sdn. Bhd."",
""Add1"": ""Plot 76 Mukim 11, Bukit Tengah Industrial Park; Bukit Mertajam; Pulau Pinang; 14000"",
""Add2"": """",
""Add3"": """",
""Daddr1"": ""Level 21, Suite 21.01, The Gardens South Tower, Mid Valley City, Lingkaran Syed Putra, ; Kuala Lumpur; Wilayah Persekutuan; 59200"",
""Daddr2"": """",
""Daddr3"": """",
""Attn"": """",
""Phone"": """",
""PhoneA"": """",
""Fax"": """",
""Area"": """",
""Agent"": """",
""Email"": """",
""CurrCode"": ""MYR"",
""Business"": """",
""Term"": ""30 Days""
},
""2"": {
""CustomerCode"": ""CUST00002"",
""Name"": ""Intel Technology Sdn Bhd"",
""Add1"": ""Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa; Bayan Lepas; Pulau Pinang; 11900"",
""Add2"": """",
""Add3"": """",
""Daddr1"": ""1st Flr,Standard Chartered Bank Chambers,Lebuh Pantai;Penang; Kuala Lumpur; Wilayah Persekutuan; 10300"",
""Daddr2"": """",
""Daddr3"": """",
""Attn"": """",
""Phone"": """",
""PhoneA"": """",
""Fax"": """",
""Area"": """",
""Agent"": """",
""Email"": """",
""CurrCode"": ""MYR"",
""Business"": """",
""Term"": ""30 Days""
},
""3"": {
""CustomerCode"": ""CUST00003"",
""Name"": ""Petronas Carigali Sdn. Bhd."",
""Add1"": ""Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,; Kuala Lumpur; Wilayah Persekutuan; 50088"",
""Add2"": """",
""Add3"": """",
""Daddr1"": ""Tower 1,Petronas Twin Towers,K.L.City Centre; Kuala Lumpur; Wilayah Persekutuan; 50088"",
""Daddr2"": """",
""Daddr3"": """",
""Attn"": """",
""Phone"": """",
""PhoneA"": """",
""Fax"": """",
""Area"": """",
""Agent"": """",
""Email"": """",
""CurrCode"": ""MYR"",
""Business"": """",
""Term"": ""30 Days""
}
}
}
}";
CustomerRootObject obj = JsonConvert.DeserializeObject<CustomerRootObject>(json);
foreach (KeyValuePair<int, CustomerInfo> kvp in obj.data.customers)
{
Console.WriteLine("----- Customer " + kvp.Key + " -----");
Console.WriteLine("CustomerCode: " + kvp.Value.CustomerCode);
Console.WriteLine("Name: " + kvp.Value.Name);
Console.WriteLine("Add1: " + kvp.Value.Add1.Replace(";", "\r\n"));
}
Console.WriteLine("\nPress a key to Exit...");
Console.ReadKey();
}
public class CustomerInfo
{
public string CustomerCode { get; set; }
public string Name { get; set; }
public string Add1 { get; set; }
public string Add2 { get; set; }
public string Add3 { get; set; }
public string Daddr1 { get; set; }
public string Daddr2 { get; set; }
public string Daddr3 { get; set; }
public string Attn { get; set; }
public string Phone { get; set; }
public string PhoneA { get; set; }
public string Fax { get; set; }
public string Area { get; set; }
public string Agent { get; set; }
public string Email { get; set; }
public string CurrCode { get; set; }
public string Business { get; set; }
public string Term { get; set; }
}
public class Data
{
public string mode { get; set; }
[JsonProperty("data")]
public Dictionary<int, CustomerInfo> customers { get; set; }
}
public class CustomerRootObject
{
public bool success { get; set; }
public string message { get; set; }
public Data data { get; set; }
}
}
Output:
----- Customer 1 -----
CustomerCode: CUST00001
Name: Dell Asia Pacific Sdn. Bhd.
Add1: Plot 76 Mukim 11, Bukit Tengah Industrial Park
Bukit Mertajam
Pulau Pinang
14000
----- Customer 2 -----
CustomerCode: CUST00002
Name: Intel Technology Sdn Bhd
Add1: Bayan Lepas Free Industrial Zone,Phase 3,Halaman Kampung Jawa
Bayan Lepas
Pulau Pinang
11900
----- Customer 3 -----
CustomerCode: CUST00003
Name: Petronas Carigali Sdn. Bhd.
Add1: Tower 1, Petronas Twin Towers,Kuala Lumpur City Centre,
Kuala Lumpur
Wilayah Persekutuan
50088
Upvotes: 4
Reputation: 4652
It looks like your JSON is not formed correctly (or you class doesn't have the correct structure). Which one can you change?
First, like @Tobberoth said, you should not be deserializing to a list.
CustomerRootObject customer = JsonConvert.DeserializeObject<CustomerRootObject> (json)
You "list" of data is not at the CustomerRootObject level - it is at the CustomerInfo level.
public class CustomerData
{
public List<CustomerInfo> customerInfo { get; set; }
}
Second, you need to change this class:
public class Data
{
public CustomerData data { get; set; }
}
Your JSON property is called data. You need to change one or the other.
Upvotes: 0