Morning all,
Im using Newtonsoft.JSON to deserialize a JSON response from Import Io. I have successfully used http://json2csharp.com/ to build a data model successfully in one instance to gain access to the data, however in that instance only one JSON object was returned. I am now working on a new piece of JSON that is being returned with multiple objects and i'm hitting issues with the following error message:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[OSP.Shop_DTO+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'url', line 1, position 7."
My JSON looks like this:
{
"url": "http://www.myurl.com/-shop-item.html",
"result": {
"extractorData": {
"url": "http://www.myurl.com/-shop-item.html",
"resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$36.00"
}
],
"BN_shop_2KG": [
{
"text": "$69"
}
],
"BN_shop_3KG": [
{
"text": "$97"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $97"
}
],
"BN_shop_4KG": [
{
"text": "$124"
}
],
"BN_shop_5KG": [
{
"text": "$149"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "1db49f66afc2f234cb5ab470f0c39e0c",
"statusCode": 200,
"timestamp": 1476432605275
},
"timestamp": 1476432606594,
"sequenceNumber": 0
}
}{
"url": "http://www.myurl.com/-shop-concentrate.html",
"result": {
"extractorData": {
"url": "http://www.myurl.com/-shop-concentrate.html",
"resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$27.00"
}
],
"BN_shop_2KG": [
{
"text": "$49"
}
],
"BN_shop_3KG": [
{
"text": "$69"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $69"
}
],
"BN_shop_4KG": [
{
"text": "$84"
}
],
"BN_shop_5KG": [
{
"text": "$99"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "dd4837cc7d0085eae005243c7bd8ca8a",
"statusCode": 200,
"timestamp": 1476432604237
},
"timestamp": 1476432605124,
"sequenceNumber": 1
}
}{
"url": "http://www.myurl.comshop-matrix.html",
"result": {
"extractorData": {
"url": "http://www.myurl.comshop-matrix.html",
"resourceId": "deee30ffa0098e017a06c0e0c805e133",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$29.00"
}
],
"BN_shop_2KG": [
{
"text": "$56"
}
],
"BN_shop_3KG": [
{
"text": "$79"
}
],
"BN_VEGE_5KG": [
{
"text": "3 KG = $79"
}
],
"BN_shop_4KG": [
{
"text": "$99"
}
],
"BN_shop_5KG": [
{
"text": "$119"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "deee30ffa0098e017a06c0e0c805e133",
"statusCode": 200,
"timestamp": 1476432602408
},
"timestamp": 1476432603204,
"sequenceNumber": 2
}
}{
"url": "http://www.myurl.comsoy-shop-item.html",
"result": {
"extractorData": {
"url": "http://www.myurl.comsoy-shop-item.html",
"resourceId": "5593aad40f95ba868626e47a1b550813",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$25.00"
}
],
"BN_shop_2KG": [
{
"text": "$45"
}
],
"BN_shop_3KG": [
{
"text": "$89"
}
],
"BN_VEGE_5KG": [
{
"text": "5 KG = $89"
}
],
"BN_shop_4KG": [
{
"text": "$175"
}
],
"BN_shop_5KG": [
{
"text": "$339"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "5593aad40f95ba868626e47a1b550813",
"statusCode": 200,
"timestamp": 1476432602479
},
"timestamp": 1476432603847,
"sequenceNumber": 3
}
}{
"url": "http://www.myurl.compea-shop.html",
"result": {
"extractorData": {
"url": "http://www.myurl.compea-shop.html",
"resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
"data": [
{
"group": [
{
"BN_shop_1KG": [
{
"text": "$25.00"
}
],
"BN_shop_2KG": [
{
"text": "$45"
}
],
"BN_shop_3KG": [
{
"text": "$89"
}
],
"BN_VEGE_5KG": [
{
"text": "5 KG = $89"
}
],
"BN_shop_4KG": [
{
"text": "$169"
}
],
"BN_shop_5KG": [
{
"text": "$319"
}
]
}
]
}
]
},
"pageData": {
"resourceId": "f91e05d0265ab5a5f7f948c57a05bced",
"statusCode": 200,
"timestamp": 1476432605227
},
"timestamp": 1476432606451,
"sequenceNumber": 4
}
}
Model looks like this:
public class BNShop1KG
{
public string text { get; set; }
}
public class BNShop2KG
{
public string text { get; set; }
}
public class BNShop3KG
{
public string text { get; set; }
}
public class BNVEGE5KG
{
public string text { get; set; }
}
public class BNShop4KG
{
public string text { get; set; }
}
public class BNShop5KG
{
public string text { get; set; }
}
public class Group
{
public List<BNShop1KG> BN_shop_1KG { get; set; }
public List<BNShop2KG> BN_shop_2KG { get; set; }
public List<BNShop3KG> BN_shop_3KG { get; set; }
public List<BNVEGE5KG> BN_VEGE_5KG { get; set; }
public List<BNShop4KG> BN_shop_4KG { get; set; }
public List<BNShop5KG> BN_shop_5KG { get; set; }
}
public class Datum
{
public List<Group> group { get; set; }
}
public class ExtractorData
{
public string url { get; set; }
public string resourceId { get; set; }
public List<Datum> data { get; set; }
}
public class PageData
{
public string resourceId { get; set; }
public int statusCode { get; set; }
public long timestamp { get; set; }
}
public class Result
{
public ExtractorData extractorData { get; set; }
public PageData pageData { get; set; }
public long timestamp { get; set; }
public int sequenceNumber { get; set; }
}
public class RootObject
{
public string url { get; set; }
public Result result { get; set; }
}
In my code i'm trying to get access to the 'text' value in 'BN_shop_1KG' from the first JSON object. This item:
"BN_shop_1KG": [
{
"text": "$36.00"
}
I've tried the following in my code, but am receiving the error outlined above. From what I understand reading some previous threads about this is that I will need to use a deserialize into List to handle JSON received in this manner:
List<Shop_DTO.RootObject> obj = JsonConvert.DeserializeObject<List<Shop_DTO.RootObject>>(_rawHtmlResult);
var price = obj.First().result.extractorData.data[0].group[0].BN_shop_1kg[0].text;
Debug.WriteLine("Price for item {0}", price);
Any help would be hugely appreciated, i've been stuck on this for a couple of days :(
The Json file is invalid. Since it contains multiple Json object, it should be a Json array which needs to be wrapped by [], and another thing is between each Json object, you need ","
var jsonText = #"[{
""url"": ""http://www.myurl.com/-shop-item.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.com/-shop-item.html"",
""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$36.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$69""
}
],
""BN_shop_3KG"": [
{
""text"": ""$97""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $97""
}
],
""BN_shop_4KG"": [
{
""text"": ""$124""
}
],
""BN_shop_5KG"": [
{
""text"": ""$149""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""1db49f66afc2f234cb5ab470f0c39e0c"",
""statusCode"": 200,
""timestamp"": 1476432605275
},
""timestamp"": 1476432606594,
""sequenceNumber"": 0
}
},{
""url"": ""http://www.myurl.com/-shop-concentrate.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.com/-shop-concentrate.html"",
""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$27.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$49""
}
],
""BN_shop_3KG"": [
{
""text"": ""$69""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $69""
}
],
""BN_shop_4KG"": [
{
""text"": ""$84""
}
],
""BN_shop_5KG"": [
{
""text"": ""$99""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""dd4837cc7d0085eae005243c7bd8ca8a"",
""statusCode"": 200,
""timestamp"": 1476432604237
},
""timestamp"": 1476432605124,
""sequenceNumber"": 1
}
},{
""url"": ""http://www.myurl.comshop-matrix.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.comshop-matrix.html"",
""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$29.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$56""
}
],
""BN_shop_3KG"": [
{
""text"": ""$79""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""3 KG = $79""
}
],
""BN_shop_4KG"": [
{
""text"": ""$99""
}
],
""BN_shop_5KG"": [
{
""text"": ""$119""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""deee30ffa0098e017a06c0e0c805e133"",
""statusCode"": 200,
""timestamp"": 1476432602408
},
""timestamp"": 1476432603204,
""sequenceNumber"": 2
}
},{
""url"": ""http://www.myurl.comsoy-shop-item.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.comsoy-shop-item.html"",
""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$25.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$45""
}
],
""BN_shop_3KG"": [
{
""text"": ""$89""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""5 KG = $89""
}
],
""BN_shop_4KG"": [
{
""text"": ""$175""
}
],
""BN_shop_5KG"": [
{
""text"": ""$339""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""5593aad40f95ba868626e47a1b550813"",
""statusCode"": 200,
""timestamp"": 1476432602479
},
""timestamp"": 1476432603847,
""sequenceNumber"": 3
}
},{
""url"": ""http://www.myurl.compea-shop.html"",
""result"": {
""extractorData"": {
""url"": ""http://www.myurl.compea-shop.html"",
""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
""data"": [
{
""group"": [
{
""BN_shop_1KG"": [
{
""text"": ""$25.00""
}
],
""BN_shop_2KG"": [
{
""text"": ""$45""
}
],
""BN_shop_3KG"": [
{
""text"": ""$89""
}
],
""BN_VEGE_5KG"": [
{
""text"": ""5 KG = $89""
}
],
""BN_shop_4KG"": [
{
""text"": ""$169""
}
],
""BN_shop_5KG"": [
{
""text"": ""$319""
}
]
}
]
}
]
},
""pageData"": {
""resourceId"": ""f91e05d0265ab5a5f7f948c57a05bced"",
""statusCode"": 200,
""timestamp"": 1476432605227
},
""timestamp"": 1476432606451,
""sequenceNumber"": 4
}
}]"
Then you can use your code to get the price. Btw, there is a typo in your code, BN_shop_1kg is supposed to be BN_shop_1KG
Related
I have the documents in the below format. I would like to count production deployments from the collection. Mongo Playground link is also attached here.
A document is considered as a production deployment when ANY of the following is true.
deployments.steps.environments.name contains Production OR Prod OR Prd
deployments.steps.stages contains Production OR Prod OR Prd
Any help on incorporating the above condition into the query to calculate TotalCount, SucceededCount etc. please?
Update: I have updated the query here. Am I right?
[
{
"productId": "613a5114b24382575e7e7668",
"deployments": [
{
"projectId": "613a5083b24382575e7e765f",
"title": "Release-4",
"steps": [
{
"releaseId": 8168,
"title": "UnitTest-Release-004",
"environments": [
{
"envId": 61553,
"name": "Production"
}
],
"stages": []
},
{
"releaseId": 7376,
"title": "UnitTest-Release-005",
"environments": [],
"stages": [
"Prod"
]
}
]
}
],
"createdAt": ISODate("2021-11-03T07:55:57.486Z"),
"deploymentStatus": "Succeeded",
"completedAt": ISODate("2021-11-03T07:29:00.907Z"),
"startedAt": ISODate("2021-11-03T07:26:53.761Z"),
},
{
"productId": "613a5114b24382575e7e7668",
"deployments": [
{
"projectId": "613a5083b24382575e7e765f",
"title": "Release-4",
"steps": [
{
"releaseId": 8168,
"title": "UnitTest-Release-004",
"environments": [
{
"envId": 61553,
"name": "Production"
}
],
"stages": []
},
{
"releaseId": 7376,
"title": "UnitTest-Release-005",
"environments": [],
"stages": []
}
]
}
],
"createdAt": ISODate("2021-11-03T07:55:57.486Z"),
"deploymentStatus": "Failed",
"completedAt": ISODate("2021-11-03T07:29:00.907Z"),
"startedAt": ISODate("2021-11-03T07:26:53.761Z"),
}
]
Here is the query.
db.collection.aggregate([
{
$match: {
$and: [
{
"createdAt": {
$gte: ISODate("2020-11-01")
}
},
{
"createdAt": {
$lte: ISODate("2021-11-17")
}
}
],
$or: [
{
"deployments.steps.environments.name": {
"$in": [
"Prd",
"Prod",
"Production"
]
}
},
{
"deployments.steps.stages.name": {
"$in": [
"Prd",
"Prod",
"Production"
]
}
}
]
}
},
{
$group: {
_id: "$productId",
TotalCount: {
$sum: 1
},
SucceededCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Succeeded"
]
},
"then": 1,
"else": 0
}
}
},
FailedCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Failed"
]
},
"then": 1,
"else": 0
}
}
},
CancelledCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"Cancelled"
]
},
"then": 1,
"else": 0
}
}
},
NotStartedCount: {
$sum: {
"$cond": {
"if": {
$eq: [
"$deploymentStatus",
"NotStarted"
]
},
"then": 1,
"else": 0
}
}
}
}
}
])
MongoPlayground
$cond - if - then - else = without quotes
I am writing a tool to work with resume.json files (project) and am creating a Json Schema to validate user input. I'm trying to automate this with Json.Net.Schema but the output always makes all properties required, regardless of whether the properties have the [Required] or [JsonRequired] attributes.
Schema Generation Code
var generator = new JSchemaGenerator
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName
};
var schema = generator.Generate(typeof(Resume));
var sb = new StringBuilder();
schema.WriteTo(new JsonTextWriter(
new IndentedTextWriter(
new StringWriter(sb),
" ")
)
{
Formatting = Formatting.Indented,
IndentChar = ' ', Indentation = 2,
QuoteName = true
}, new JSchemaWriterSettings
{
Version = SchemaVersion.Draft7
});
File.WriteAllText("E:\\resume.schema", sb.ToString());
Resume Class (and children)
public class Resume
{
[Required]
public Basics Basics { get; set; }
public Work[] Work { get; set; }
public Volunteer[] Volunteer { get; set; }
public Education[] Education { get; set; }
public Award[] Awards { get; set; }
public Publication[] Publications { get; set; }
public Skill[] Skills { get; set; }
public Language[] Languages { get; set; }
public Interest[] Interests { get; set; }
public Reference[] References { get; set; }
}
public class Award
{
[Required]
public string Title { get; set; }
public string Date { get; set; }
public string Awarder { get; set; }
public string Summary { get; set; }
}
public class Basics
{
[Required]
public string Name { get; set; }
public string Label { get; set; }
public Uri Picture { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public string Summary { get; set; }
public Location Location { get; set; }
public Profile[] Profiles { get; set; }
}
public class Education
{
[Required]
public string Institution { get; set; }
public string Area { get; set; }
public string StudyType { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string Gpa { get; set; }
public string[] Courses { get; set; }
}
public class Interest
{
[Required]
public string Name { get; set; }
public string[] Keywords { get; set; }
}
public class Language
{
[Required]
public string language { get; set; }
[Required]
public string Fluency { get; set; }
}
public class Location
{
public string Address { get; set; }
[Required]
public string PostalCode { get; set; }
[Required]
public string City { get; set; }
[Required]
public string CountryCode { get; set; }
public string Region { get; set; }
}
public class Profile
{
[Required]
public string Network { get; set; }
[Required]
public string Username { get; set; }
[Required]
public string Url { get; set; }
}
public class Publication
{
[Required]
public string Name { get; set; }
public string Publisher { get; set; }
public string ReleaseDate { get; set; }
public string Website { get; set; }
public string Summary { get; set; }
}
public class Reference
{
[Required]
public string Name { get; set; }
public string reference { get; set; }
}
public class Skill
{
[Required]
public string Name { get; set; }
[Required]
public string Level { get; set; }
public string[] Keywords { get; set; }
}
public class Volunteer
{
[Required]
public string Organization { get; set; }
[Required]
public string Position { get; set; }
public string Website { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string Summary { get; set; }
public string[] Highlights { get; set; }
}
public class Work
{
[Required]
public string Company { get; set; }
[Required]
public string Position { get; set; }
public string Website { get; set; }
[Required]
public string StartDate { get; set; }
public string EndDate { get; set; }
public string Summary { get; set; }
public string[] Highlights { get; set; }
}
Current Output
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "Resume",
"definitions": {
"Award": {
"$id": "Award",
"type": [
"object",
"null"
],
"properties": {
"title": {
"type": "string"
},
"date": {
"type": [
"string",
"null"
]
},
"awarder": {
"type": [
"string",
"null"
]
},
"summary": {
"type": [
"string",
"null"
]
}
},
"required": [
"title",
"date",
"awarder",
"summary"
]
},
"Basics": {
"$id": "Basics",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"label": {
"type": [
"string",
"null"
]
},
"picture": {
"type": [
"string",
"null"
],
"format": "uri"
},
"email": {
"type": [
"string",
"null"
]
},
"phone": {
"type": [
"string",
"null"
]
},
"website": {
"type": [
"string",
"null"
]
},
"summary": {
"type": [
"string",
"null"
]
},
"location": {
"$id": "Location",
"type": [
"object",
"null"
],
"properties": {
"address": {
"type": [
"string",
"null"
]
},
"postalCode": {
"type": "string"
},
"city": {
"type": "string"
},
"countryCode": {
"type": "string"
},
"region": {
"type": [
"string",
"null"
]
}
},
"required": [
"address",
"postalCode",
"city",
"countryCode",
"region"
]
},
"profiles": {
"$id": "Profile[]",
"type": [
"array",
"null"
],
"items": {
"$id": "Profile",
"type": [
"object",
"null"
],
"properties": {
"network": {
"type": "string"
},
"username": {
"type": "string"
},
"url": {
"type": "string"
}
},
"required": [
"network",
"username",
"url"
]
}
}
},
"required": [
"name",
"label",
"picture",
"email",
"phone",
"website",
"summary",
"location",
"profiles"
]
},
"Education": {
"$id": "Education",
"type": [
"object",
"null"
],
"properties": {
"institution": {
"type": "string"
},
"area": {
"type": [
"string",
"null"
]
},
"studyType": {
"type": [
"string",
"null"
]
},
"startDate": {
"type": [
"string",
"null"
]
},
"endDate": {
"type": [
"string",
"null"
]
},
"gpa": {
"type": [
"string",
"null"
]
},
"courses": {
"$id": "String[]",
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"institution",
"area",
"studyType",
"startDate",
"endDate",
"gpa",
"courses"
]
},
"Interest": {
"$id": "Interest",
"type": [
"object",
"null"
],
"properties": {
"name": {
"type": "string"
},
"keywords": {
"$id": "String[]",
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"name",
"keywords"
]
},
"Language": {
"$id": "Language",
"type": [
"object",
"null"
],
"properties": {
"language": {
"type": "string"
},
"fluency": {
"type": "string"
}
},
"required": [
"language",
"fluency"
]
},
"Location": {
"$ref": "Location"
},
"Profile": {
"$ref": "Profile"
},
"Publication": {
"$id": "Publication",
"type": [
"object",
"null"
],
"properties": {
"name": {
"type": "string"
},
"publisher": {
"type": [
"string",
"null"
]
},
"releaseDate": {
"type": [
"string",
"null"
]
},
"website": {
"type": [
"string",
"null"
]
},
"summary": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"publisher",
"releaseDate",
"website",
"summary"
]
},
"Reference": {
"$id": "Reference",
"type": [
"object",
"null"
],
"properties": {
"name": {
"type": "string"
},
"reference": {
"type": [
"string",
"null"
]
}
},
"required": [
"name",
"reference"
]
},
"Skill": {
"$id": "Skill",
"type": [
"object",
"null"
],
"properties": {
"name": {
"type": "string"
},
"level": {
"type": "string"
},
"keywords": {
"$id": "String[]",
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"name",
"level",
"keywords"
]
},
"Volunteer": {
"$id": "Volunteer",
"type": [
"object",
"null"
],
"properties": {
"organization": {
"type": "string"
},
"position": {
"type": "string"
},
"website": {
"type": [
"string",
"null"
]
},
"startDate": {
"type": [
"string",
"null"
]
},
"endDate": {
"type": [
"string",
"null"
]
},
"summary": {
"type": [
"string",
"null"
]
},
"highlights": {
"$id": "String[]",
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"organization",
"position",
"website",
"startDate",
"endDate",
"summary",
"highlights"
]
},
"Work": {
"$id": "Work",
"type": [
"object",
"null"
],
"properties": {
"company": {
"type": "string"
},
"position": {
"type": "string"
},
"website": {
"type": [
"string",
"null"
]
},
"startDate": {
"type": "string"
},
"endDate": {
"type": [
"string",
"null"
]
},
"summary": {
"type": [
"string",
"null"
]
},
"highlights": {
"$id": "String[]",
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
}
},
"required": [
"company",
"position",
"website",
"startDate",
"endDate",
"summary",
"highlights"
]
}
},
"type": "object",
"properties": {
"basics": {
"$ref": "Basics"
},
"work": {
"$id": "Work[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Work"
}
},
"volunteer": {
"$id": "Volunteer[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Volunteer"
}
},
"education": {
"$id": "Education[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Education"
}
},
"awards": {
"$id": "Award[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Award"
}
},
"publications": {
"$id": "Publication[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Publication"
}
},
"skills": {
"$id": "Skill[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Skill"
}
},
"languages": {
"$id": "Language[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Language"
}
},
"interests": {
"$id": "Interest[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Interest"
}
},
"references": {
"$id": "Reference[]",
"type": [
"array",
"null"
],
"items": {
"$ref": "Reference"
}
}
},
"required": [
"basics",
"work",
"volunteer",
"education",
"awards",
"publications",
"skills",
"languages",
"interests",
"references"
]
}
How do I get the schema generator to honor the Required attributes?
JSchemaGenerator has a property DefaultRequired:
Gets or sets the default required state of schemas.
For some reason, the default value for this property is Required.AllowNull, as shown in the source:
public JSchemaGenerator()
{
_schemaReferenceHandling = SchemaReferenceHandling.Objects;
_defaultRequired = Required.AllowNull;
}
If you change it to Required.Default, only those properties explicitly marked as required, e.g. with [Required] or [JsonProperty(Required = Required.Always)], will be listed as required in the schema:
var generator = new JSchemaGenerator
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
SchemaIdGenerationHandling = SchemaIdGenerationHandling.TypeName,
DefaultRequired = Required.Default,
};
The default value for DefaultRequired isn't documented, but probably should be. You might open an issue with Newtonsoft requesting a clarification to the documentation.
Demo fiddle here.
I'm parsing js file containing object values to C# objects. For now - I've converted JS code to JSON and then tried to convert to C# object.
I'm having problem with coming up to idea of how to generate objects in C#. I've tried doing multiple various tries, mostly with Dictionaries (Dictionary<string,[object]). I Googled, visited SO in multiple questions, no success for now - all my ideas resulted in null object.
Important note - I can't change the source of JS, can change anything after that.
Latest objects idea:
public class SingleFarm
{
public List<string> Modules { get; set; }
public List<string> Servers { get; set; }
}
public class SingleEnv
{
public Dictionary<string, SingleFarm> Farms { get; set; }
}
public class FarmsModel
{
public Dictionary<string, SingleEnv> FarmsModel { get; set; }
}
Parsing code:
var farmsText = File.ReadAllText(filePath);
//using Jurassic
var engine = new ScriptEngine();
var result = engine.Evaluate(farmsText);
var json = JSONObject.Stringify(engine, result);
var parsed = JsonConvert.DeserializeObject<FarmsModel>(json);
JS file source:
var environments = {};
environments['ENV1'] = {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
};
environments['ENV2'] = {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
};
environments['ENV3'] = {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
};
JSON looks as follows:
{
"ENV1": {
"WWW": {
"Modules": [
"module21"
],
"Servers": [
"a-1"
]
}
},
"ENV2": {
"FARM1": {
"Modules": [
"module41"
],
"Servers": [
"s1",
"s2"
]
},
"FARM2": {
"Modules": [
"module11"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module1"
],
"Servers": [
""
]
}
},
"ENV3": {
"FARM1": {
"Modules": [
"module10"
],
"Servers": [
"server1"
]
},
"FARM2": {
"Modules": [
"module22"
],
"Servers": [
""
]
},
"FARM3": {
"Modules": [
"module33"
],
"Servers": [
"server3"
]
}
}
}
Do you have any ideas?
You shouldn't be trying to serialize dictionaries to objects since it will try to map the property names.
If you use
var parsed = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, SingleFarm>>>(json);
It should work. Fiddle
I want to deserialize JSON response received by Telegram Bot API by getUpdate() method.
JSON data:
{
"ok": true,
"result": [
{
"update_id": 920493886,
"message": {
"message_id": 123,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457375,
"text": "Aata aala"
}
},
{
"update_id": 920493887,
"message": {
"message_id": 124,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457387,
"text": "Jeva tuzyakadun reply aala tevha"
}
},
{
"update_id": 920493888,
"message": {
"message_id": 125,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457443,
"text": "Deposite"
}
},
{
"update_id": 920493889,
"message": {
"message_id": 127,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457645,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493890,
"message": {
"message_id": 128,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457670,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493891,
"message": {
"message_id": 130,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457848,
"text": "Deposite"
}
},
{
"update_id": 920493892,
"message": {
"message_id": 132,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457883,
"text": "Deposite"
}
},
{
"update_id": 920493893,
"message": {
"message_id": 133,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472468407,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493894,
"message": {
"message_id": 134,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472473070,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
}
]
}
I have generated classes from json2csharp
public class From
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string username { get; set; }
}
public class Chat
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string type { get; set; }
public string username { get; set; }
}
public class Entity
{
public string type { get; set; }
public int offset { get; set; }
public int length { get; set; }
}
public class Message
{
public int message_id { get; set; }
public From from { get; set; }
public Chat chat { get; set; }
public int date { get; set; }
public string text { get; set; }
public List<Entity> entities { get; set; }
}
public class Result
{
public int update_id { get; set; }
public Message message { get; set; }
}
public class RootObject
{
public bool ok { get; set; }
public List<Result> result { get; set; }
}
using newtonsoft to deserialization
var d = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data);
what to do next? how to put this all at work? I am confused please help.
To deserialization use this code:
RootObject ro = JsonConvert.DeserializeObject<RootObject>(rcvd_data);
And in ro you have all data.
EXAMPLE
bool ok = ro.ok;
foreach(Result r in ro.result)
{
int uId = r.update_id;
Message m = r.message;
int msgId = m.message_id;
}
string data = #"{""ok"":true,""result"":[{""update_id"":920493886,...";
RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(data);
foreach (Result result in ro.result)
{
//two example fields
Console.WriteLine("update_id= " + result.update_id);
Console.WriteLine("message text= "+result.message.text);
}
Based on: https://yts.am/api/v2/list_movies.json (Source: https://yts.am/api)
I used: http://json2csharp.com/ to create classes
Copy and paste the result of the get request of postman to json2csharp to automatically create the classes that you need
And once created, i used them on a Service like this:
private string URL = "https://yts.am/api/v2/list_movies.json";
public async Task<List<Movie>> GetMovies()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(URL);
RootObject lista = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
List<Movie> todoes = lista.data.movies;
return todoes;
}
I'm using Newtonsoft.Json
Try this way
var d = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(rcvd_data);
then d will contain list of RootObject. By iterating using foreach you can get all the properties of RootObject model
I am calling a REST service to get back a JSON structure.
The error I get is
Test method BarPanda.Web.Services.Test.PosServiceTests.GetMenu threw exception:
System.MissingMethodException: No parameterless constructor defined for this object.
The original JSON is as follows (partial)
{
"count": 3,
"limit": 50,
"_links": {
"self": {
"etag": "b49a27c7e7c663af8d6a736e24fac7f5",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/" ,
"profile": "https://panel.omnivore.io/docs/api#category_list"
}
},
"_embedded": {
"categories": [
{
"id": "AdiRjiAp",
"name": "Drinks",
"_links": {
"items": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/items/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_list"
},
"self": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/" ,
"profile": "https://panel.omnivore.io/docs/api#category_retrieve"
}
},
"_embedded": {
"items": [
{
"id": "gki84ia9",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Soda",
"open": false,
"pos_id": "gki84ia9",
"price": 150,
"price_levels": [
{
"id": "Byineidy",
"price": 150
},
{
"id": "g4T4dTBj",
"price": 200
},
{
"id": "K6czkc8b",
"price": 250
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list"
},
"self": {
"etag": "c59b380aed5c1f33915b028b739df955",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"
}
}
},
{
"id": "doTaLTyg",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Orange Juice",
"open": false,
"pos_id": "doTaLTyg",
"price": 175,
"price_levels": [
{
"id": "L4iqKid8",
"price": 175
},
{
"id": "K6T8MTzb",
"price": 300
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list"
},
"self": {
"etag": "d3ae9754edb321f18e192ebea446baeb",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"
}
}
}
]
}
},
I am trying to deserialize it with the following code and object classes
var response = _client.Execute(request);
var converter = new JsonDeserializer();
var menu = converter.Deserialize<PosMenu>(response);
PosMenu
[DataContract]
public class PosMenu
{
[DataMember]
public int VenueId { get; set; }
[DataMember]
public int count { get; set; }
[DataMember]
public PosMenuEmbedded _embedded { get; set; }
}
PosMenuEmbedded
[DataContract]
public class PosMenuEmbedded
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public PosMenuCategory[] categories { get; set; }
[DataMember]
public int PosMenuId { get; set; }
}
PosMenuCategory
[DataContract]
public class PosMenuCategory
{
}
Note: I have taken all properties out of this class for now just to see if I could get it working with a blank class, but alas not.
If I comment out the line in PosMenuEmbedded
public PosMenuCategory[] categories { get; set; }
It succeeds. If I put it back in, it fails, even with an empty class.
Can anyone suggest why this might be?
[DataMember]
public List<PosMenuCategory> categories { get; set; }