getting json byte array in list - asp.net

I got null value for byte array in json web service when deserializing.but it returns byte array when invoked from browser.
C# Code:
var url = re.DownloadString("XXXX/ListService.svc/lstFooddtl/1/21");
var a = JsonConvert.DeserializeObject<FooddtlResult>(url);
In a i got null for food_photo...when i deserialize...
c# Class:
public class photo
{
public byte[] food_photo { get; set; }
}
public class Food
{
public int food_id { get; set; }
public string food_name { get; set; }
public photo[] food_photo { get; set; }
public string unitcost { get; set; }
}
public class FooddtlResult
{
public Food[] Result { get; set; }
}
{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[255,216,255,224,0,....217],"unitcost":null}]}

Your model does not match the JSON. For correct deserialization change your Food class to
public class Food
{
public int food_id { get; set; }
public string food_name { get; set; }
public byte[] food_photo { get; set; }
public string unitcost { get; set; }
}
and remove the photo class.
Otherwise, if you want to keep the model structure, then correct JSON should be:
{"Result":[{"food_id":"61","food_name":"Idli","food_photo":[{"food_photo":[255,216,255,224,0,....217]},{"food_photo":[255,...]},...],"unitcost":null}]}

Related

How to make request body for generic list as request parameter in wcf service

[DataContract]
public class OrderDetails
{
[DataMember(IsRequired = true)]
public string OrderReference { get; set; }
[DataMember]
public List<Product> Product { get; set; }
}
[MessageContract]
public class Product
{
[MessageBodyMember]
public string Number { get; set; }
[MessageBodyMember]
public string Code { get; set; }
[MessageBodyMember]
public string Quantity { get; set; }
}
urn:OrderDetails
urn:OrderReference?</urn:OrderReference>
urn:Product
urn:Product
urn:Number?</urn:LineNumber>
urn:Code?</urn:ProductCode>
urn:Quantity?</urn:Quantity>
</urn:Product>
</urn:Product> </urn:OrderDetails>
But I want below output urn:OrderDetails
urn:OrderReferencedf</urn:OrderReference>
urn:Product
urn:Numberf</urn:LineNumber>
urn:Codedf</urn:ProductCode>
</urn:Product>
urn:Product
urn:Numberf</urn:LineNumber>
urn:Codedf</urn:ProductCode>
</urn:Product> </urn:OrderDetails> so what I change in SerivceContract?

Refit me showing an error when initializing

I am trying to consume a service using Refit and I have the following problem. I was looking at the documentation but I can't find a solution and I don't know if I should do anything else.
This is my model
public partial class Global
{
[JsonProperty("updated")]
public long Updated { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("countryInfo")]
public List<CountryInfo> CountryInfo { get; set; }
}
My second class:
public partial class CountryInfo
{
[JsonProperty("_id")]
public long? Id { get; set; }
[JsonProperty("iso2")]
public string Iso2 { get; set; }
[JsonProperty("iso3")]
public string Iso3 { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("long")]
public double Long { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
Application with Refit
Link : https://corona.lmao.ninja/v2/countries
[Get("/v2/countries")]
Task<CountryInfo> GetCountry();
In my viewModel
private async Task GetTaskInterf()
{
var webApp = RestService.For<ICovidInterface>("https://corona.lmao.ninja");
var result = await webApp.GetGlobalCountry();
}
At the time of initialization

Is it possible to get List<T> without some objects?

Need to get all data from List without status and date
public class Locations
{
public string CompanyCode { get; set; }
public string SupplierId { get; set; }
public string LocationName { get; set; }
public int Status { get; set; }
public DteTime date{ get; set; }
public int ResultCode { get; set; }
public string ResultMessage { get; set; }
}
I getting all locations in below list, I want to avoid status and date from the results
List<Locations> results
You could make use Anonymous Types
results.Select(x=>new{x.CompanyCode,x.SupplierId,x.LocationName,x.ResultCode,x.ResultMessage})
Alternatively, you could create a Custom class with only the desired property and project it using Linq as shown in the example with Anonymous Types
or you can use deriving from class/interface and then cast to it ;)
you can do something like this:
public interface ILocationsPoorData
{
public string CompanyCode { get; set; }
public string SupplierId { get; set; }
public string LocationName { get; set; }
public int ResultCode { get; set; }
public string ResultMessage { get; set; }
}
class Locations:ILocationsPoorData
{
public string CompanyCode { get; set; }
public string SupplierId { get; set; }
public string LocationName { get; set; }
public int Status { get; set; }
public DteTime date{ get; set; }
public int ResultCode { get; set; }
public string ResultMessage { get; set; }
}
and in the end, when you'll have list of locations, you can do sth like this:
var newList = locationsList.select(x=> x as ILocationsPoorData)
or
var newList = locationsList.Cast<ILocationsPoorData>();
or you can create class instead of interface and do the same ;)
or you can use anonymous types like in previous answer
or you can create some value tuple instead of anonymous types ;)

ServiceStack AutoQuery join use

After reading the documentation, I am not sure but I have come to the conclusion that when creating QueryDb, you cannot choose the columns to join by? And I am under the impression, you must have DTO object to copy to? You cannot copy to a regular object or a dynamic object?
public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm, object>, ILeftJoin<MailResponseDetailOrm, MailResponseOrm> { }
Can anyone provide any insight on joining my MailResponseOrm to MailResponseDetailOrm. MailResponseDetailOrm has 5 fields namely the Email address. And I would like MailResponseOrm to be joined to it by Email as well. I also, for good measure do not want to alter either columnname. Would I have to create a custom implementation or a service to do this?
UPDATE
Here is my code as posted below:
[Alias("MailReportsDetail")]
public class MailResponseDetailOrm
{
public string Email { get; set; }
public int ID { get; set; }
[Alias("RespDate")]
public DateTime? AddedDateTime { get; set; }
[Alias("DLReport")]
public string Action { get; set; }
public string ActionDetail { get; set; }
public string IP { get; set; }
public string UserAgent { get; set; }
public string EmailReferrer { get; set; }
}
[Alias("MailReports")]
public class MailResponseOrm
{
public int ID { get; set; }
public string Email { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Company { get; set; }
public string Contact { get; set; }
public string Country { get; set; }
[Alias("LastMail")]
public DateTime? ModifiedDateTime { get; set; }
[Alias("LastReport")]
public string Action { get; set; }
public DateTime? OptOut { get; set; }
public string Part { get; set; }
public string Phone { get; set; }
public string PostalCode { get; set; }
public string Source { get; set; }
public string State { get; set; }
public string Title { get; set; }
#region Obsolete
[Obsolete]
public string Class { get; set; }
[Obsolete]
public string IP { get; set; }
#endregion
}
public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm> { }
public class MyQueryServices : Service
{
public IAutoQueryDb AutoQuery { get; set; }
// Override with custom implementation
public object Any(SampleAutoQueryDb query)
{
var q = AutoQuery.CreateQuery(query, base.Request);
q.Join<MailResponseDetailOrm, MailResponseOrm>((x, y) => x.Email == y.Email)
// .Select<MailResponseDetailOrm, MailResponseOrm>((x, y) => new { x.ID, y.Email })
;
return AutoQuery.Execute(query, q);
}
}
Joins in AutoQuery needs to use OrmLite's Joins Reference conventions and all AutoQuery Services results are returned in a Typed DTO, which by default is the table being queried or you can use the QueryDb<From,Into> base class to return a custom result of columns from multiple joined tables.
You would need to use a Custom AutoQuery Implementation or your own Service implementation if you need customizations beyond this, e.g:
public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm> { }
public class MyQueryServices : Service
{
public IAutoQueryDb AutoQuery { get; set; }
// Override with custom implementation
public object Any(SampleAutoQueryDb query)
{
var q = AutoQuery.CreateQuery(query, base.Request);
q.Join<MailResponseDetailOrm,MailResponseOrm>((x, y) => x.Email == y.Email);
return AutoQuery.Execute(query, q);
}
}
// The query to join 2 objects on field names not specifically set in the class.
var q = Db.From<MailResponseDetailOrm>().Join<MailResponseDetailOrm>(x,y) => x.Email = y.Email);
// Run the query
var results = Db.Select(q);

Parse Complex JSON to ASP.NET MVC 3 using jQuery

I have to post this javascript object to asp.net controller:
The model in server side is:
public class UserExperience
{
public class competences
{
public string id { get; set; }
public string level { get; set; }
public string name { get; set; }
public bool isNew { get; set; }
public bool isSaved { get; set; }
}
public long id { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }
public string projectName { get; set; }
public string company { get; set; }
public string customerIndustry { get; set; }
public string jobTitle { get; set; }
public string projectDescription { get; set; }
public string responsabilities { get; set; }
public List<competences> competence { get; set; }
public List<int> deletedCompetences { get; set; }
public bool isNew { get; set; }
public bool isSaved { get; set; }
}
I tried to send the json like this:
$.ajax("/candidate/saveUserExperience", {
data : JSON.stringify({ue: is.Candidate.BO.userExperience[id]}),
//dataType: "text",
contentType : "application/json",
type : 'POST'
})
And this is the controller where I receive it:
public JsonResult saveUserExperience(UserExperience ue)
{
bool success;
success = true;
return Json(new { success, ue }, JsonRequestBehavior.AllowGet);
}
And I receive a null object.
I tried to do with:
UserExperience userExperience = new JavaScriptSerializer().Deserialize<UserExperience>(ue);
and I do not receive the competence subclass object.
Is there a solution to this problem ?
Thanks.
Why are you trying to pass is.Candidate.BO.userExperience[id] when you expect UserExperience object? Just write data: ue in ajax call parameters,

Resources