I am new to C# and .Net core. I wish to get a string from a setter _modelDetails, but I am facing an issue. Below is its detailed description. Any help is greatly appreciated!
I have a json as below:
"VehicleDetails": [
{
"vehicleId": "vehicleId",
"vehicleName": "vehicleName",
"version": "Version",
"modelDetails": [
{
"year": "1900",
"color": "blue",
"transaction": [
{
"transactionName": "transactionName1"
},
{
"transactionName": "transactionName2"
}
]
},
{
"year": "2000",
"color": "green",
"transaction": [
{
"transactionName": "transactionName3"
},
{
"transactionName": "transactionName4"
}
]
}
]
}
]
For the json, below are part of the models:
public class VehicleDetails : IValidatableObject {
public List<string> _modelDetails
public List<MobileDetails> modelDetails
{
get => _modelDetails;
set
{
_modelDetails = value;
string transactionName = "";
_modelDetails?.ForEach(model =>
{
foreach (var transaction in model.Transaction)
{
transactionName = transaction.DisplayName;
break;
}
_modelDetails = model.TagId ";" model.DisplayName ";" transactionName;
Console.WriteLine(_modelDetails);
});
}
}
}
public class ModelDetails :IValidatableObject
{
private List<Transaction> _transactions;
public List<Transaction> Transaction {
get => _transactions;
set
{
_transactions = value;
}
}
public List<IValidatableObject> GetChildObjects()
{
var childObjects = new List<IValidatableObject>() ;
if(Transaction?.Count > 0) childObjects.AddRange(Transaction);
return childObjects.Count > 0 ? childObjects : null;
}
}
public class Transaction: IValidatableObject
{
public string TransactionName { get; set; }
public List<IValidatableObject> GetChildObjects()
{
return null;
}
}
I need the _modelDetails to return a semicolon separated strings as below:
1900;blue;transactionName1
1900;blue;transactionName2
2000;green;transactionName3
2000;green;transactionName4
But right now its just returning these two strings:
1900;blue;transactionName1 and
2000;green;transactionName3
I seems simple logically but I am not getting where I am going wrong. Can anyone please help me?
CodePudding user response:
try this
List<VehicleDetail> vehicleDetails = JsonConvert.DeserializeObject<Root>(json).VehicleDetails;
List<string> modelDetails = vehicleDetails[0].ModelDetails;
output
1900;blue;transactionName1
1900;blue;transactionName2
2000;green;transactionName3
2000;green;transactionName4
And I had to fix your classes to deserialize json
public class Root
{
public List<VehicleDetail> VehicleDetails { get; set; }
}
public class VehicleDetail : IValidatableObject
{
public string vehicleId { get; set; }
public string vehicleName { get; set; }
public string version { get; set; }
[JsonProperty("modelDetails")]
private List<ModelDetail> _modelDetailsList;
private List<string> modelDetails=new List<string>();
public List<string> ModelDetails
{
get
{
if (modelDetails.Count == 0)
{
foreach (var model in _modelDetailsList)
foreach (var transaction in model.Transaction)
modelDetails.Add(model.year ";" model.color ";" transaction.TransactionName "\n");
}
return modelDetails;
}
}
}
public class ModelDetail : IValidatableObject
{
public string year { get; set; }
public string color { get; set; }
private List<Transaction> _transaction;
public List<Transaction> Transaction
{
get => _transaction;
set
{
_transaction = value;
}
}
public List<IValidatableObject> GetChildObjects()
{
var childObjects = new List<IValidatableObject>();
if (Transaction?.Count > 0) childObjects.AddRange(Transaction);
return childObjects.Count > 0 ? childObjects : null;
}
}
public class Transaction : IValidatableObject
{
public string TransactionName { get; set; }
public List<IValidatableObject> GetChildObjects()
{
return null;
}
}
CodePudding user response:
well you say only process the first transaction
foreach (var transaction in model.Transaction)
{
transactionName = transaction.DisplayName;
break; <<<<=======
}
probably you want
foreach (var transaction in model.Transaction)
{
transactionName = transaction.DisplayName;
_modelDetails = model.TagId ";" model.DisplayName ";" transactionName;
Console.WriteLine(_modelDetails);
}