Hello I have one json text inside this text there is one key which starts with $
string jsonText="{\"Version\":\"1.1\",\"Documents\":[{\"DocumentState\":\"Correct\",\"DocumentData\":{\"Name\":\"test\",\"$type\":\"Document\",\"Fields\":[{\"Name\":\"CustomerFullName\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"0\",\"RecognizedValue\":\"\",\"Value\":\"\"},{\"Name\":\"CustomerBirthDate\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"DocumentNumber\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"0000000000\",\"RecognizedValue\":\"\",\"Value\":\"\"},{\"Name\":\"CustomerIsMarried\",\"$type\":\"Checkmark\",\"IsSuspicious\":false,\"Value\":true},{\"Name\":\"CustomerCounty\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"CustomerArea\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"CustomerAddress\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"11000000\",\"RecognizedValue\":\"\",\"Value\":\"\"},{\"Name\":\"DocumentGUID\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"DocumentProposalID\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"DocumentCountry\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"000\",\"RecognizedValue\":\"FRA\",\"Value\":\"FRA\"},{\"Name\":\"DocumentCurrency\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"000\",\"RecognizedValue\":\"EUR\",\"Value\":\"EUR\"},{\"Name\":\"DocumentTo\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"DocumentFrom\",\"$type\":\"Text\",\"Value\":\"\"},{\"Name\":\"DocumentTotalNumberOfPages\",\"$type\":\"Text\",\"SuspiciousSymbols\":\"0\",\"RecognizedValue\":\"1\",\"Value\":\"1\"}]}}]}";
Console.WriteLine(jsonText);
var documentResult = JsonConvert.DeserializeObject<DocumentDTO>(jsonText);
and my class objects are below
public class DocumentDTO
{
public string Version { get; set; }
public List<DocumentInfo> Documents { get; set; }
}
public class DocumentInfo
{
public string DocumentState { get; set; }
public DocumentData DocumentData { get; set; }
public string DocumentAsBase64 { get; set; }
}
public class DocumentData
{
public string Name { get; set; }
[JsonPropertyName("$type")]
public string type { get; set; }
public List<DocumentField> Fields { get; set; }
}
public class DocumentField
{
public string Name { get; set; }
[JsonPropertyName("$type")]
public string type { get; set; }
public string SuspiciousSymbols { get; set; }
public string RecognizedValue { get; set; }
public string Value { get; set; }
}
but it is not working not converting $type to type. How can I solve this problem ?
Thanks in advance
Kind Regards
That's because you're using JsonConvert from Newtonsoft.Json library and are marking property with JsonPropertyName attribute from System.Text.Json.Serialization. The two just don't play well together. Try replacing your JsonPropertyName with JsonProperty attribute from Newtonsoft.Json and it should work.
public class DocumentData
{
public string Name { get; set; }
[JsonProperty("$type")]
public string Type { get; set; }
public List<DocumentField> Fields { get; set; }
}
public class DocumentField
{
public string Name { get; set; }
[JsonProperty("$type")]
public string Type { get; set; }
public string SuspiciousSymbols { get; set; }
public string RecognizedValue { get; set; }
public string Value { get; set; }
}
This question already has answers here:
convert a list of objects from one type to another using lambda expression
(14 answers)
How to cast one compound custom type to another
(2 answers)
C# "cannot implicitly convert type" same model properties
(4 answers)
Closed 2 years ago.
Based on the module name I want to return different lists of different types but problem is that I cannot change the return type for each so I wrote a generic class that has the same properties but it doesn't work that way. How to achieve it.
public List<Models.TasksList> Get(string module)
{
//var query= "";
using (var context = new TasksPlanEntities())
{
if (module == "PEMS")
{
var query = from st in context.PIRTaskLists
select st;
return query.ToList<Models.TasksList>();
else if (module == "PSVMS")
{
var query = from st in context.PSVMS_EnggData
select st;
return query.ToList<Models.TasksList>();
}
}}
TasksList
public class TasksList
{
public int OperationID { get; set; }
public string PIRCode { get; set; }
public string TaskName { get; set; }
public string OperationNo { get; set; }
public string OperationCode { get; set; }
public string OperationDescription { get; set; }
//public static explicit operator TasksList(ObjectResult<GetTaskPlans_EPICOR_Result> v)
//{
// throw new NotImplementedException();
//}
public string Resource { get; set; }
public string NDTResources { get; set; }
public float Duration { get; set; }
public string RequiredEquipmentStatus { get; set; }
public string TimeBaseInterval { get; set; }
public DateTime StartDate { get; set; }
public DateTime LastInspDate { get; set; }
public DateTime NextInspDate { get; set; }
public string FunctionalLocation { get; set; }
}
throws error
Cannot implicitly convert to system.collection.generic.list<Models.TasksList>
Even though the TasksList class has exactly the same names and all. I tried everything but doesn't work out. Why it is so?
Why this is happening since it's the same structure.
Update:
public virtual DbSet<PIRTaskList> PIRTaskLists { get; set; }
public partial class PIRTaskList
{
public int OperationID { get; set; }
public string PIRCode { get; set; }
public string TaskName { get; set; }
public Nullable<int> OperationNo { get; set; }
public Nullable<int> Counter { get; set; }
public string OperationCode { get; set; }
public string OperationDescription { get; set; }
public string Resource { get; set; }
public string NDTResources { get; set; }
public Nullable<double> Duration { get; set; }
public string RequiredEquipmentStatus { get; set; }
public string TimeBaseInterval { get; set; }
public string InspectionType { get; set; }
public string TimeBaseInterval_UOME { get; set; }
public string Description { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> LastInspDate { get; set; }
public string Remarks { get; set; }
public Nullable<System.DateTime> NextInspDate { get; set; }
public Nullable<System.DateTime> IntermediateDue { get; set; }
public Nullable<System.DateTime> ThroughDue { get; set; }
public string DueDate { get; set; }
public string GroupStatusID { get; set; }
public Nullable<int> StatusID { get; set; }
public Nullable<int> EnggDataID { get; set; }
public string FunctionalLocation { get; set; }
public Nullable<System.DateTime> CreatedOn { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<System.DateTime> ModifiedOn { get; set; }
public Nullable<int> ModifiedBy { get; set; }
public string PersonName { get; set; }
public string CostPerHour_Dollar { get; set; }
}
This is very simple, there's nothing that allows a list of PIRTaskList to be implicitly converted into a list of TasksList. The fact that the classes share the same properties names is irrelevant.
You have many options and which one you choose depends entirely on your project/team and knowledge you have about the system that we don't.
Use query.Select(x => new TasksList ...).ToList() to manually transform the items
Use an implicit/explicit conversion with a .Select
Use a library like AutoMapper
Use a List<dynamic>/List<object> (and lose type safety)
Probably more options that I cannot think of right now
I am using multiple levels of JSON data coming into php from a C# application, as in:
public class User_Group
{
public int ID_UserGroup { get; set; }
public string Name_UserGroup { get; set; }
public int UserID { get; set; }
}
public class User_Role
{
public int ID_User { get; set; }
public string Role_User { get; set; }
public string User_Role_Description { get; set; }
public List<User_Group> UserGroup { get; set; }
}
public class Stand_Orte
{
public int ID { get; set; }
public string Bezeichnung { get; set; }
public List<Modul> modul { get; set; }
}
public class Modul
{
public string ID { get; set; }
public string Seriennummer { get; set; }
public string Bezeichnung { get; set; }
public string StandortID { get; set; }
public List<Mess_Kanal> MessKanal { get; set; }
}
public class Mess_Kanal
{
public string ID { get; set; }
public string ModulID { get; set; }
public List<LogMess_Daten> LogMessDaten { get; set; }
}
public class LogMess_Daten
{
public string KanalID { get; set; }
public string Zeitstempel { get; set; }
}
public class RootObject
{
public int ID_Project { get; set; }
public string Name_Project { get; set; }
public int Receiver_ID { get; set; }
public string Receiver_Name { get; set; }
public int UserID { get; set; }
public User_Role UserRole { get; set; }
public Stand_Orte Standorte { get; set; }
}
I have an issue with accessing the 3rd level data elements.
For Eg., I am able to get the values uptil Project-> Stand_Orte-> Modul. but after that Project-> Stand_Orte-> Modul-> MessKanal Throws an error in PHP as " Trying to get property of non-object ".
I tried the following:
$phpArray['project']->Standorte[0]->modul[0]->MessKanal[0]->ID
$messkanals=$phpArray->Standorte->modul->MessKanal;
And then used a "foreach ($messkanals as $messkanal)" to insert MessKanal data into MYSQL.
it gives me the following errors respectively.
Cannot use object of type stdClass as array
Trying to get property of non-object
does anyone have any idea?
Thanks,
Revathy
public static List<IndianAppStore_GetAllAppsByLanguage_ResultCache> GetAllApps(bool initialized, string language)
{
List<IndianAppStore_GetAllAppsByLanguage_ResultCache> objApp = new List<IndianAppStore_GetAllAppsByLanguage_ResultCache>();
List<IndianAppStore_GetAllAppsByLanguage_Result> objApps = new List<IndianAppStore_GetAllAppsByLanguage_Result>();
if (initialized == false)
{
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x); // Error
objApp = admin.getAllAppsByLanguage(language).ToList();
}
else
{
}
}
public static List<TResult> ListCopy<TSource, TResult>(List<TSource> input, Func<TSource, TResult> convertFunction)
{
return input.Select(x => convertFunction(x)).ToList();
}
My Class
public class IndianAppStore_GetAllAppsByLanguage_ResultCache
{
public long AppId { get; set; }
public string AppName { get; set; }
public string AppDisplayName { get; set; }
public string AppDetails { get; set; }
public string AppImageURL { get; set; }
public byte[] AppImageData { get; set; }
public long CategoryId { get; set; }
public Nullable<long> SubCategoryId { get; set; }
public string AppCreatedBy { get; set; }
public System.DateTime AppCreatedOn { get; set; }
public string AppModifiedBy { get; set; }
public Nullable<System.DateTime> AppModifiedOn { get; set; }
public Nullable<bool> isDeleted { get; set; }
public Nullable<bool> isPromotional { get; set; }
public string GenderTarget { get; set; }
public Nullable<long> CountryId { get; set; }
public Nullable<long> StateId { get; set; }
public Nullable<long> AgeLimitId { get; set; }
public Nullable<int> AppMinAge { get; set; }
public Nullable<int> AppMaxAge { get; set; }
}
I am trying to convert one generic class to another but getting this error
IndianAppStore_GetAllAppsByLanguage_Result and IndianAppStore_GetAllAppsByLanguage_ResultCache are different types and you cannot cast the first type to the other as you are doing in this statement:
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x);
If the types have the same structure you should probably just have one instead of two types. Otherwise you will have to copy the data from the first type to the other. E.g.:
var t = ListCopy(objApps, x => new IndianAppStore_GetAllAppsByLanguage_ResultCache {
AppId = x.AppId,
AppName = x.AppName,
...
});
This becomes tedious very quickly and one option is to use a library like AutoMapper to automate the process.
I want to select certain fields from my domain model, and group by a field within the domain model, and put the results into a ViewModel - however, I can't get the Linq syntax correct.
My Model is:
public class Offer
{
public int OfferId { get; set; }
public string Inclusions { get; set; }
public string Property { get; set; }
public bool IncludeInOffer { get; set; }
public Guid guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My ViewModel is:
public class OfferVMList
{
public string guid { get; set; }
public int NumPeople { get; set; }
public int NumNights { get; set; }
public DateTime ArrivalDate { get; set; }
public string CustomerName { get; set; }
public string EmailAddress { get; set; }
public DateTime OfferCreatedOn { get; set; }
public string Email { get; set; }
public string OfferReference { get; set; }
}
My Linq is:
OfferVMList item = db.Offers
.GroupBy(x => x.OfferReference)
.Select(ct => new OfferVMList
{
NumPeople = ct.NumPeople
})
.OrderBy(x => x.ArrivalDate);
However, the error message is:
System.Linq.IGrouping<string,FGBS.Models.Offer>' does not contain a definition for 'NumPeople' and no extension method 'NumPeople' accepting a first argument of type 'System.Linq.IGrouping<string,FGBS.Models.Offer>' could be found (are you missing a using directive or an assembly reference?)'
Can anyone see where I'm going wrong please?
Thank you
Check this:
var item = db.Offers
.OrderBy(x => x.ArrivalDate);
.GroupBy(x => x.OfferReference)
.Select(ct => ct.Select(x => new OfferVMList {
NumPeople = x.NumPeople
});