I was learning how to consume API's and got stuck with the error "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'NbaApi.Models.Standard' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly." Thank you in advance!
I have this json
{
"_internal": {
"pubDateTime": "2020-04-03 13:11:16.692 EDT",
"igorPath": "S3,1585933850069,1585933857347|router,1585933857347,1585933857352|domUpdater,1585933857484,1585933875507|feedProducer,1585933875618,1585933885453",
"xslt": "NBA/xsl/league/roster/marty_active_players.xsl",
"xsltForceRecompile": "true",
"xsltInCache": "false",
"xsltCompileTimeMillis": "253",
"xsltTransformTimeMillis": "7800",
"consolidatedDomKey": "prod__transform__marty_active_players__1989425396229",
"endToEndTimeMillis": "35384"
},
"league": {
"standard": [
{
"firstName": "Steven",
"lastName": "Adams",
"temporaryDisplayName": "Adams, Steven",
"personId": "203500",
"teamId": "1610612760",
"jersey": "12",
"isActive": true,
"pos": "C",
"heightFeet": "6",
"heightInches": "11",
"heightMeters": "2.11",
"weightPounds": "265",
"weightKilograms": "120.2",
"dateOfBirthUTC": "1993-07-20",
"teamSitesOnly": {
"playerCode": "steven_adams",
"posFull": "Center",
"displayAffiliation": "Pittsburgh/New Zealand",
"freeAgentCode": ""
},
"teams": [
{
"teamId": "1610612760",
"seasonStart": "2013",
"seasonEnd": "2019"
}
],
"draft": {
"teamId": "1610612760",
"pickNum": "12",
"roundNum": "1",
"seasonYear": "2013"
},
"nbaDebutYear": "2013",
"yearsPro": "6",
"collegeName": "Pittsburgh",
"lastAffiliation": "Pittsburgh/New Zealand",
"country": "New Zealand"
}
]
}
}
I have these model classes
public class Data
{
public League League { get; set; }
}
public class League
{
public Standard Standard { get; set; }
}
public class Standard
{
public List<Player> players { get; set; }
}
public class Player
{
public string FirstName { get; set; }
public string LastName { get; set; }
//other parameters are also in the model
}
And I use this method to consume the api and pass it to the model
class Program
{
static void Main(string[] args)
{
var data = new List<Data>();
Player player = new Player();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://data.nba.net/10s/prod/v1/2019/players.json");
//HTTP GET
var responseTask = client.GetAsync("");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsAsync<List<Data>>();
readTask.Wait();
data = readTask.Result;
foreach (var item in data)
{
var league = item.League;
var standard = league.Standard;
var players = standard.players;
player = players.Where(p => p.LastName == "Adams").First();
}
}
else
{
throw new Exception(result.ReasonPhrase);
}
}
Console.WriteLine(player.LastName);
Console.ReadLine();
}
}
You need change your model as below
public class TeamSitesOnly
{
public string playerCode { get; set; }
public string posFull { get; set; }
public string displayAffiliation { get; set; }
public string freeAgentCode { get; set; }
}
public class Team
{
public string teamId { get; set; }
public string seasonStart { get; set; }
public string seasonEnd { get; set; }
}
public class Draft
{
public string teamId { get; set; }
public string pickNum { get; set; }
public string roundNum { get; set; }
public string seasonYear { get; set; }
}
public class Standard
{
public string firstName { get; set; }
public string lastName { get; set; }
public string temporaryDisplayName { get; set; }
public string personId { get; set; }
public string teamId { get; set; }
public string jersey { get; set; }
public bool isActive { get; set; }
public string pos { get; set; }
public string heightFeet { get; set; }
public string heightInches { get; set; }
public string heightMeters { get; set; }
public string weightPounds { get; set; }
public string weightKilograms { get; set; }
public string dateOfBirthUTC { get; set; }
public TeamSitesOnly teamSitesOnly { get; set; }
public List<Team> teams { get; set; }
public Draft draft { get; set; }
public string nbaDebutYear { get; set; }
public string yearsPro { get; set; }
public string collegeName { get; set; }
public string lastAffiliation { get; set; }
public string country { get; set; }
}
public class League
{
public List<Standard> standard { get; set; }
}
public class Data
{
public League league { get; set; }
}
You can use online tool to convert JSON object to C# class structure by http://json2csharp.com/
Change your model (since standard is an array, not an object):
public class League
{
public List<Player> Standard { get; set; }
}
The response object has structure:
{
"league": {
"standard": [...]
}
}
While your current model tries to deserialize:
{
"league": {
"standard": {
"players": [...]
}
}
}
Related
My POCO classes:
[Table]
public class Product
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<CategoryProduct> CategoryProducts { get; set; }
}
public class CategoryProduct
{
public int CategoryId { get; set; }
public int ProductId { get; set; }
public Category Category { get; set; }
public Product Product { get; set; }
}
[Table]
public class Category
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<CategoryProduct> CategoryProducts { get; set; }
}
Here's a function for inserting new records:
async Task CreateProduct(Product dto)
{
await ctx.Products.AddAsync(dto);
await ctx.SaveChangesAsync();
}
In dto I pass the following JSON:
{
"name": "Gräff Stettin",
"categoryProducts": [{
"categoryId": 1,
"productId": 1,
"category": {
"id": 1,
"name": "Drinks"
}
}, {
"categoryId": 2,
"productId": 1,
"category": {
"id": 2,
"name": "Alcohol"
}
}
]
}
As a result, at SaveChangesAsync() I get an exception with message regarding attempt to insert already existing Category. Tracing shows the following query:
INSERT INTO "Category" ("Id", "Name") VALUES (#p0, #p1);
How should I change my CreateProduct() method to avoid attempts to add categories with already existing categoryId?
you can use AutoMapper and ignore category =
var config = new MapperConfiguration(cfg => cfg.CreateMap<categoryProductsDto,categoryProducts>())
.ForMember(u => u.Category, options => options.Ignore());
and use mapper =
var mapper = config.CreateMapper();
categoryProducts entity = mapper.Map<categoryProducts>(input);
await _categoryProductsRepository.InsertAndGetIdAsync(entity);
I have two classes:
public class MDBProducts
{
public string _id { get; set; }
public string order_number { get; set; }
[BsonIgnoreIfNull]
public List<Classes.MDBParts[]> partsId { get; set; }
}
public class MDBParts
{
public string _id { get; set; }
public string accountCode { get; set; }
}
With this lookup command mongodb embedded whole document information.
string param = "{$lookup: { from: 'Parts',localField: 'order_number',foreignField: 'accountCode',as:'partsId'} }";
BsonDocument document = BsonDocument.Parse(param);
var pipeline = new[] { document };
var result = Classes.MdB.connectDbProducts().Aggregate<MDBProducts>(pipeline).ToList();
I want to add to "partsId" only MDBParts class _id field. May you help me notice, how to do that?
References between Parts and Products collections
`var lookup1 = new BsonDocument
{
{
"$lookup",
new BsonDocument
{
{ "from", "Parts" },
{ "localField", "order_number" },
{ "foreignField", "productNumber" },
{ "as", "partsId" }
}
},
};
var lookup2 = new BsonDocument
{
{
"$project",
new BsonDocument
{
{ "partsId", "$partsId._id" },
{ "id", 1 }
}
}
};
var pipeline = new[] { lookup1, lookup2 };
var result = connectDbProducts().Aggregate<Products>(pipeline).ToList();
foreach (var item in result)
{
if (item.partsId.Count != 0)
{
var filterID = Builders<Products>.Filter.Eq(x => x._id, item._id);
var setTableTop = Builders<Products>.Update.Set(x => x.partsId, item.partsId);
connectDbProducts().UpdateOne(filterID, setTableTop);
}
else
{
var filterID = Builders<Products>.Filter.Eq(x => x._id, item._id);
var setTableTop = Builders<Products>.Update.Unset(x => x.partsId);
connectDbProducts().UpdateOne(filterID, setTableTop);
}
}`
Products Class
public class Products
{
public object _id { get; set; }
public string electricity_based_on { get; set; }
public double? full_price { get; set; }
public DateTime manufacturing_date { get; set; }
public string order_code { get; set; }
public string order_comment_names { get; set; }
public string order_name { get; set; }
public string order_number { get; set; }
public double? order_quantity { get; set; }
public double? unit_price { get; set; }
[BsonIgnoreIfNull]
public string user_name { get; set; }
[BsonIgnoreIfNull]
public DateTime user_checkTime { get; set; }
[BsonIgnoreIfNull]
public object order_comments { get; set; }
[BsonIgnoreIfNull]
public string tabletop_letter { get; set; }
[BsonIgnoreIfNull]
public string sub_3_4 { get; set; }
[BsonIgnoreIfNull]
public List<string> partsId { get; set; }
}
Connection to DB
public static IMongoCollection<Products> connectDbProducts()
{
try
{
MongoClient dbClient = new MongoClient(Properties.Resource1.mongoDB);
var db = dbClient.GetDatabase("Manufacturing");
var collection = db.GetCollection<Products>("Products");
return collection;
}
catch (Exception toMongoDBParts)
{
WriteLogFile("--MongoDB connection to Parts collection--", toMongoDBParts.StackTrace, toMongoDBParts.Message);
throw;
}
}
I am new to mongodb i am trying to insert a data into my collection using mongodb shell
User.cs
public class User
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("userName")]
public string? UserName { get; set; }
[BsonElement("workDay")]
public IEnumerable<WorkDay>? WorkDay { get; set; }
[BsonElement("emailAddress")]
public string? EmailAddress { get; set; }
[BsonElement("sendEventsOutsideWorkHours")]
public bool? SendEventsOutsideWorkHours { get; set; }
[BsonElement("sendReportOutsideWorkHours")]
public bool? SendReportsOutsideWorkHours { get; set; }
[BsonElement("timeZoneOffset")]
public int? TimeZoneOffset { get; set; }
}
Workday.cs
public class WorkDay
[BsonElement("dayOfWeek")]
public DayOfWeek DayOfWeek { get; set; }
[BsonElement("startTime")]
public WorkDayBoundary _StartTime { get; set; }
public TimeSpan StartTime
{
get
{
return new TimeSpan(_StartTime.Hours, _StartTime.Minutes, 0);
}
}
[BsonElement("endTime")]
public WorkDayBoundary _EndTime { get; set; }
public TimeSpan EndTime
{
get
{
return new TimeSpan(_EndTime.Hours, _EndTime.Minutes, 0);
}
}
}
public class WorkDayBoundary
{
[BsonElement("hours")]
public int Hours { get; set; }
[BsonElement("minutes")]
public int Minutes { get; set; }
}
and here is my code in mongodb shell
db.Users.insertMany([{
'userName': 'Testing123',
'workDay': {
'dayOfWeek': 0,
'startTime': { 'hours': 5, 'minutes': 23 },
'endTime': { 'hours': 5, 'minutes': 23 }
},
'emailAddress': 'thisismymail#email.com',
'sendEventsOutsideWorkHours': 1,
'sendReportOutsideWorkHours': 1,
'timeZoneOffset': 5.00
},
{
'userName': 'Something999',
'workDay': {
'dayOfWeek': 1,
'startTime': { 'hours': 1, 'minutes': 55 },
'endTime': { 'hours': 10, 'minutes': 33 }
},
'emailAddress': 'somethingas#email#email.com',
'sendEventsOutsideWorkHours': 0,
'sendReportOutsideWorkHours': 0,
'timeZoneOffset': 2.00
}
])
and while executing from .netcore application it was throwing an exception as
"FormatException: Cannot deserialize a 'List<WorkDay>' from BsonType 'Document'."
How can I ignore a navigation property without using FluentAPI in the onModelCreating for the below example:
Table Layout:
My Classes are below. When I use my contact to get results for PmActionDetail, the WorkTask table includes the AssetTypeWorkTask results, which is not the desired result.
public partial class PmActionDetail
{
public int PmActionDetlId { get; set; }
public int? AssetTypeWorkTaskId { get; set; }
public virtual AssetTypeWorkTask AssetTypeWorkTask { get; set; }
}
public partial class AssetTypeWorkTask
{
public AssetTypeWorkTask()
{
AssetPmTypeWorkTask = new HashSet<AssetPmTypeWorkTask>();
}
public int Id { get; set; }
public int? AssetTypeId { get; set; }
public int WorkTaskId { get; set; }
public virtual AssetType AssetType { get; set; }
public virtual AssetWorkTask WorkTask { get; set; }
public virtual ICollection<AssetPmTypeWorkTask> AssetPmTypeWorkTask { get; set; }
public virtual ICollection<PmActionDetail> PmActionDetail { get; set; }
}
public partial class AssetType
{
public AssetType()
{
AssetTypeWorkTask = new HashSet<AssetTypeWorkTask>();
}
public int AssetTypeId { get; set; }
public string AssetTypeDescription { get; set; }
public virtual ICollection<AssetTypeWorkTask> AssetTypeWorkTask { get; set; }
}
public partial class AssetWorkTask
{
public int Id { get; set; }
public string Desc { get; set; }
public virtual ICollection<AssetTypeWorkTask> AssetTypeWorkTask { get; set; }
}
Here is the context query I am running:
return await _context.PmActionDetail.Include("AssetTypeWorkTask").Include("AssetTypeWorkTask.WorkTask")
.Include("AssetTypeWorkTask.AssetType").ToListAsync();
These are the results. I do not want to show the relationship for assetTypeWorkTask under "workTask". I understand why it is perhaps showing it, since it is part of the relationship. However, its not the result I need.
[
{
"pmActionDetlId": 6008,
"pmActionHeadId": 1517,
"assetTypeWorkTaskId": 29,
"assetTypeWorkTask": {
"id": 29,
"assetTypeId": 31,
"workTaskId": 8,
"assetType": {
"assetTypeId": 31,
"assetTypeCategory": "ROOM",
"assetTypeWorkTask": []
},
"workTask": {
"id": 8,
"desc": "PM",
"active": 1,
"assetTypeWorkTask": [
{
"id": 20,
"assetTypeId": 20,
"workTaskId": 8,
"assetType": {
"assetTypeId": 20,
"assetTypeCategory": "CASEGOODS",
}
},
{
"id": 21,
"assetTypeId": 21,
"workTaskId": 8,
"assetType": {
"assetTypeId": 21,
"assetTypeCategory": "APPLIANCES",
},
}
]
},
"assetPmTypeWorkTask": [],
"pmActionDetail": null
}
}
]
I want to get the response of the Model & reference model in json format from the Restful WCF service..
My model as below
// Role : User
[DataContract]
public class User
{
[DataMember]
public int Id {get; set;}
[DataMember]
public String Email { get; set; }
[DataMember]
public String Name { get; set; }
[DataMember]
public String Password { get; set; }
[DataMember]
public String Designation { get; set; }
}
// Role : Model
[DataContract]
public class Role
{
public Role()
{ }
[DataMember]
public int Id { get; set; }
[DataMember]
public String Name { get; set; }
}
// UserRole : Model
[DataContract]
public class UserRole
{
public UserRole()
{}
[DataMember]
public int Id { get; set; }
[DataMember]
public int UserId { get; set; }
[DataMember]
public int RoleId { get; set; }
[DataMember]
public int Level { get; set; }
}
Here what exactly I want to do.
when any client call method
Method: AssignRoleToUser with Form parameter "UserID" & "RoleId"
at that time I inserting new record to UserRole Entity and return object with json.
currently below JSON string output as response.
Current Output
{
"Id": 2,
"Level": 0,
"RoleId": 3,
"UserId": 2
}
I want like below
{
"Id": 2,
"Level": 0,
"RoleId": [
{
"Id": 5,
"Name": "Project Leader"
}
],
"UserId": [
{
"Designation": "System User",
"Email": "abhishek#domain.com",
"Gender": 0,
"Id": 1,
"Name": "System User",
"Password": null
}
]
}
Thanks