I am working on a lambda api, and i need to get response in object of list.
here is my model:
public class Response
{
public List<Dictionary<string, string>> FailedTenant { get; set; }
public List<Dictionary<string,string>> FailedWaterMeter { get; set; }
public List<Dictionary<string, string>> SuccessfulTenant { get; set; }
public List<Dictionary<string,string>> SuccessfulWaterMeter { get; set; }
public Response()
{
FailedTenant = new List<Dictionary<string, string>>();
FailedWaterMeter = new List<Dictionary<string, string>>();
SuccessfulTenant = new List<Dictionary<string, string>>();
SuccessfulWaterMeter = new List<Dictionary<string, string>>();
}
}
public class FailedTenant
{
public string Email { get; set; }
public string Message { get; set; }
}
public class FailedWaterMeter
{
public string MeterNumber { get; set; }
public string Message { get; set; }
}
public class SuccessfulTenant
{
public string Email { get; set; }
public string Message { get; set; }
}
public class SuccessfulWaterMeter
{
public string MeterNumber { get; set; }
public string Message { get; set; }
}}
i have implemented this in my code like this:
Response responseResult = new Response();
foreach (FailedTenant failedTenant in failedTenants)
{
Dictionary<string, string> failedTenantDictionary = new Dictionary<string, string>
{
{"Email", failedTenant.Email},
{"Message", failedTenant.Message}
};
responseResult.FailedTenant.Add(failedTenantDictionary);
}
foreach (FailedWaterMeter failedWaterMeter in failedWaterMeters)
{
Dictionary<string, string> failedWaterMeterDictionary = new Dictionary<string, string>
{
{"MeterNumber", failedWaterMeter.MeterNumber},
{"Message", failedWaterMeter.Message}
};
responseResult.FailedWaterMeter.Add(failedWaterMeterDictionary);
}
foreach (SuccessfulTenant successfulTenant in successfulTenants)
{
byte[] messageBytes = Convert.FromBase64String(successfulTenant.Message);
successfulTenant.Message = Encoding.UTF8.GetString(messageBytes);
Dictionary<string, string> successfulTenantDictionary = new Dictionary<string, string>
{
{"Email", successfulTenant.Email},
{"Message", successfulTenant.Message}
};
responseResult.SuccessfulTenant.Add(successfulTenantDictionary);
}
foreach (SuccessfulWaterMeter successfulMeter in successfulWaterMeters)
{
byte[] messageBytes = Convert.FromBase64String(successfulMeter.Message);
successfulMeter.Message = Encoding.UTF8.GetString(messageBytes);
Dictionary<string, string> successWaterMeterDictionary = new Dictionary<string, string>
{
{"Email", successfulMeter.MeterNumber},
{"Message", successfulMeter.Message}
};
responseResult.SuccessfulWaterMeter.Add(successWaterMeterDictionary);
}
string message = string.Empty;
if (responseResult.SuccessfulTenant.Count>0 && responseResult.SuccessfulWaterMeter.Count>0 && responseResult.FailedTenant.Count==0 && responseResult.FailedWaterMeter.Count == 0)
{
return BaseResponse.SuccessResponse("All Tenants Added Successfully");
}
else if(responseResult.SuccessfulTenant.Count>0 || responseResult.FailedTenant.Count>0 || responseResult.FailedWaterMeter.Count > 0 || responseResult.SuccessfulWaterMeter.Count>0)
{
string jsonResponse = JsonConvert.SerializeObject(responseResult);
return BaseResponse.FailureResponse(JsonConvert.SerializeObject("Some Tenants and Some Water Meters not added "+ " "+ jsonResponse));
}
else
{
string jsonResponse = JsonConvert.SerializeObject(responseResult.FailedTenant,Formatting.Indented);
return BaseResponse.FailureResponse(JsonConvert.SerializeObject("No Tenants Were Processed " + "Failed Tenants: " + jsonResponse));
}
and i am getting the response like this :
{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”},{“Email”:”[email protected]”,”Message”:”Email already exist,Please Use Another email!”}
but i want this in json object form like:
{“Email”:”[email protected]”,”Message”:”Email already exist”}
How will i do that , please help
RANU TANWAR is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.