Json.net JsonSchema trouble C# - c#

I am trying to Parse a schema and read an element from it however I am getting an error.
This is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Newtonsoft.Json.Schema;
namespace ConsoleApplication1
{
class Test
{
static void Main(string[] args)
{
string ingameschemaFilePath = #"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\ingameschema.txt";
string dota2schemaFilePath = #"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\dota2schema.txt";
string schemaFilePath = #"C:\Users\Andrew\Documents\GitHub\SteamBot\Bin\Debug\schema.txt";
JsonSchema dota2schema = JsonSchema.Parse(File.ReadAllText(dota2schemaFilePath));
Console.WriteLine(dota2schema.result.items.name);
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
}
And this is the error I am getting:
Error 2 'Newtonsoft.Json.Schema.JsonSchema' does not contain a definition for 'result' and no extension method 'result' accepting a first argument of type 'Newtonsoft.Json.Schema.JsonSchema' could be found (are you missing a using directive or an assembly reference?)
I am trying to follow the sample here:
http://james.newtonking.com/projects/json/help/#
Samples -> JsonSchema -> Parse Json schema
And here is the start of the schema I am trying to read from:
{
"result": {
"status": 1,
"items_game_url": "http:\/\/media.steampowered.com\/apps\/570\/scripts\/items\/items_game.d8ab2f9911cea9d7f4bce1add62c7bb83a902322.txt",
"qualities": {
"normal": 0,
"genuine": 1,
"vintage": 2,
"unusual": 3,
"unique": 4,
"community": 5,
"developer": 6,
"selfmade": 7,
"customized": 8,
"strange": 9,
"completed": 10,
"haunted": 11,
"tournament": 12,
"favored": 13
},
"originNames": [
{
"origin": 0,
"name": "Timed Drop"
},
{
"origin": 1,
"name": "Achievement"
},
{
"origin": 2,
"name": "Purchased"
},
{
"origin": 3,
"name": "Traded"
},
{
"origin": 4,
"name": "Crafted"
},
{
"origin": 5,
"name": "Store Promotion"
},
{
"origin": 6,
"name": "Gifted"
},
{
"origin": 7,
"name": "Support Granted"
},
{
"origin": 8,
"name": "Found in Crate"
},
{
"origin": 9,
"name": "Earned"
},
{
"origin": 10,
"name": "Third-Party Promotion"
},
{
"origin": 11,
"name": "Wrapped Gift"
},
{
"origin": 12,
"name": "Halloween Drop"
},
{
"origin": 13,
"name": "Steam Purchase"
},
{
"origin": 14,
"name": "Foreign Item"
},
{
"origin": 15,
"name": "CD Key"
},
{
"origin": 16,
"name": "Collection Reward"
},
{
"origin": 17,
"name": "Preview Item"
},
{
"origin": 18,
"name": "Steam Workshop Contribution"
},
{
"origin": 19,
"name": "Periodic Score Reward"
},
{
"origin": 20,
"name": "Recycling"
},
{
"origin": 21,
"name": "Tournament Drop"
},
{
"origin": 22,
"name": "Passport Reward"
},
{
"origin": 23,
"name": "Tutorial Drop"
}
]
,
"items": [
{
"name": "Riki's Dagger",
"defindex": 0,
"item_class": "dota_item_wearable",
"item_type_name": "#DOTA_WearableType_Daggers",
"item_name": "#DOTA_Item_Rikis_Dagger",
"proper_name": false,
"item_quality": 0,
"image_inventory": null,
"min_ilevel": 1,
"max_ilevel": 1,
"image_url": "",
"image_url_large": "",
"capabilities": {
"can_craft_mark": true,
"can_be_restored": true,
"strange_parts": true,
"paintable_unusual": true,
"autograph": true
I thought I did pretty much exactly what the sample did. What have I done wrong? I have searched for similar problems but have not found the answer. Also, if you could let me know the correct way of getting the Def index of "Riki's Dagger" from the json schema that would be great.

If you want to read an element from JSON.
Generate classes from your JSON using http://json2csharp.com/ or http://jsonclassgenerator.codeplex.com/ (I feel this is better).
Use JSON.net to deserialize your JSON into root class.
var ouput -JsonConvert.Deserialize<your_root_class>(JSONString);
Just read the value you want to.

Related

CamelCase for property in data response

I have one API that return that :
{
"Success": true,
"Data": [
{
"ID": 1,
"ProcedureID": 1,
"Channel": "Channel 1",
"ResolvedTime": "Anytime",
"Description": "No description"
}
],
"StatusCode": 200,
"Message": ""
}
but actually I want is :
{
"Success": true,
"Data": [
{
"id": 1,
"procedureID": 1,
"channel": "Channel 1",
"resolvedTime": "Anytime",
"description": "No description"
}
],
"StatusCode": 200,
"Message": ""
}
what are happening with my list "Data", I just want the property names of all the objects inside that list formatted in camelCase. How can I do that? I'm using EF core 6 and my Program.cs already have this line:
builder.Services.AddControllers().AddNewtonsoftJson(options =>{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;});

Mailchimp not rendering variables

I am using the Mailchimp template to send emails through Mandrill. Email being sent successfully. But the variables are not rendering. I tried lots of settings but unfortunately, it is not working. My code is looking like the following.
<p>Dear {{customerName}},</p>
<p>Thank you for using XXXXX to process your return.</p>
<p>All you have to do is bring your parcel along with this e-mail to {{pudoLocation}} at {{emailPudoAddress}}.</p>
And In the Email output, it is showing like this.
And My request JSON to API https://mandrillapp.com/api/1.0/messages/send-template.json is:-
valid json is `{
"key": "xxxxxxxxxxxxxxx",
"template_name": "TestTemplate",
"template_content": [],
"message": {
"subject": "TestEmail",
"from_email": "xxx.xx#xxxxx.in",
"from_name": "Drop2Shop",
"to": [{
"Email": "xxxxx.xxx#xxxxxx.in",
"Name": "Amrit Pannu",
"Type": "to"
}],
"headers": {
"Reply-To": "xxxxx.xxx#xxxx.in"
},
"important": false,
"track_clicks": true,
"track_opens": true,
"merge_language": "handlebars",
"global_merge_vars": [{
"name": "pudoLocation",
"content": "TRK00000028"
},
{
"name": "customerName",
"content": "TRK00000028"
}],
"tags": [""]
}
}`
Please can someone help me here what I missing in this?
Thanks and Advance
You can pass global variables in mandrill using *|VAR1|*. To pass array you can use {{handlebars}}.
The content will be like this:
<p>Dear *|customerName|*,</p>
<p>Thank you for using XXXXX to process your return.</p>
<p>All you have to do is bring your parcel along with this e-mail to *|pudoLocation|* at *|emailPudoAddress|*.</p>
The JSON should be like this only:
{
"key": "XXXXXXXXXXXXXXX",
"template_name": "XXXXXXXXXXXXXXXXXX",
"template_content": [],
"message": {
"subject": "TestEmail",
"from_email": "xxxx#xxxxxxxxxxx.in",
"from_name": "xxxxxx",
"to": [
{
"email": "xxxx#xxxxxxxxxxx.in",
"name": "xxxxx xxxxx",
"type": "to"
}
],
"headers": {
"reply_to": "xxxx.xxxx#xxxx.in"
},
"important": false,
"track_opens": true,
"track_clicks": true,
"merge_language": "handlebars",
"global_merge_vars": [
{
"name": "trackingNumber",
"content": "TRK00000028"
},
{
"name": "customerName",
"content": "Amrit Pannu"
},
{
"name": "eCommerceRetailer",
"content": "Jojo Retailer"
},
{
"name": "goodsDescription",
"content": "Tricycles, Scooters, and Similar Wheeled Toys; Dolls' Carriages"
},
{
"name": "shippingDate",
"content": "2021-07-07"
},
{
"name": "numberOfPackages",
"content": "45"
},
{
"name": "shipFromAddress",
"content": "IRELAND"
},
{
"name": "shipToAddress",
"content": "Britain"
},
{
"Name": "pudoAddress",
"Content": "USA"
},
{
"name": "shipFromContact",
"content": "Commin"
},
{
"name": "shipToContact",
"content": "Stark"
},
{
"name": "customerReference",
"content": "DF7889FSDHCB"
},
{
"name": "pudoLocation",
"content": "Denmark"
},
{
"name": "emailPudoAddress",
"content": "amrit#mailinator.com"
},
{
"name": "unsubscribePageUrl",
"content": ""
},
{
"name": "email",
"content": "amrit#mailinator.com"
},
{
"name": "scanEventTime",
"content": "12:07"
},
{
"name": "scanEventDate",
"content": "07 July 2021"
},
{
"name": "packageItems",
"content": [
{
"GoodsDescription": "869c6f67-f33d-4b30-a274-79b635d12461",
"ReturnReason": "c3b4b071-7664-4060-a39f-52c24147a1ce",
"Quantity": 3,
"Value": 6
},
{
"GoodsDescription": "5e2c625b-74e6-4e90-bb52-6595e9f3d088",
"ReturnReason": "f0949b28-f9a7-406e-8676-b8ce6c714509",
"Quantity": 3,
"Value": 14
},
{
"GoodsDescription": "cffe036a-2441-41af-82e1-130f9169924b",
"ReturnReason": "f31956b9-c24e-492e-b11f-4b4e19489b13",
"Quantity": 1,
"Value": 5
},
{
"GoodsDescription": "f8f64467-6926-4f08-9d4e-aa924dc1af35",
"ReturnReason": "d1f759b0-8e54-4119-94ee-6afe1bc49766",
"Quantity": 4,
"Value": 0
},
{
"GoodsDescription": "496f4e0e-739d-4fce-8a37-85113f27c4d4",
"ReturnReason": "b8d98439-928f-4e6c-bb56-01e6f8fb6be6",
"Quantity": 2,
"Value": 5
},
{
"GoodsDescription": "afe2986c-e51e-4cfe-9506-f9ef2afa5657",
"ReturnReason": "4e6e34f6-d921-46fb-8f4a-b04afd0856a7",
"Quantity": 4,
"Value": 12
},
{
"GoodsDescription": "13ac561a-d161-46a5-9795-8481e99a3afd",
"ReturnReason": "73f4441b-dcff-4f68-a58e-320a29a167c8",
"Quantity": 1,
"Value": 7
},
{
"GoodsDescription": "135e083b-0408-4445-9a0c-3656a46f7eab",
"ReturnReason": "325c7144-8f7d-4712-b708-1702416ca02c",
"Quantity": 2,
"Value": 13
},
{
"GoodsDescription": "ee510bfd-58ea-4707-9c52-26913b190b27",
"ReturnReason": "854629a5-289b-46c9-a072-099e2a572307",
"Quantity": 4,
"Value": 12
},
{
"GoodsDescription": "b6d6fa6e-a327-4014-b019-0c4cd0098da6",
"ReturnReason": "02394799-761c-46f0-af79-d8bc92b2c1b5",
"Quantity": 1,
"Value": 13
}
]
},
{
"name": "currentYear",
"content": 2021
}
],
"tags": null,
"inline_css": true
}
For more information, please see this: https://mailchimp.com/developer/transactional/docs/templates-dynamic-content/#dynamic-content
I'm probably too late, but if it can help someone else.
It's not working because you have to remove all the backslashes Mailchimp added when send it to Mandrill before your variables. Change : \{{customerName}} to {{customerName}}

Write to JArray in JObject

I have a JSON file.
{
"time": [
{
"id": "9999",
"name": "Foo",
"subitem": [
{
"name": "Bar",
"id": "99990",
"visible": true,
"subitem": [
{
"id": "999901",
"name": "Flex",
"visible": true
},
{
"name": "Bear",
"id": "999902",
"visible": true
},
{
"name": "James",
"id": "999903",
"visible": true
}
]
},
{
"name": "Smith",
"id": "999966",
"visible": true
},
{
"name": "John",
"id": "999933",
"visible": true
}
],
"visible": true
},
{
"name": "Doe",
"id": "1111",
"visible": true,
"subitem": [
{
"name": "Jack",
"id": "111111",
"visible": true
},
{
"name": "Wilson",
"id": "111188",
"visible": true
},
{
"name": "Andy",
"id": "111144",
"visible": true
},
{
"name": "Gibbs",
"id": "111155",
"visible": true
}
]
}
],
"name": "asdf",
"id": "13",
"visible": true
}
I also have a JObject and a method to get all the JSON data and store it in this object.
json1 = ti.GetTimeItems();
I have 2 methods in another class to write to the JSON file. Where datafolder is the path.
public void WriteToJson(JObject obj)
{
string fileName = dataFolder + "json1.json";
WriteToJson(fileName, obj);
}
private void WriteToJson(string fileName, JObject obj)
{
using (StreamWriter file = File.CreateText(fileName))
using (JsonTextWriter writer = new JsonTextWriter(file))
{
obj.WriteTo(writer);
}
}//end WriteToJson
Then i have a windows form where i want to take the text from 2 textboxes and add these to the JSON file.
Finally i have my click event
private void button1_Click_1(object sender, EventArgs e)
{
//string json = File.ReadAllText(url);
//JArray time = (JArray)json1.SelectToken("time");
json1.Add(new JObject(
new JProperty("name", textBoxName.Text),
new JProperty("id", textBoxId.Text),
new JProperty("visible", true)));
ti.WriteToJson(json1);
}
My problem is that i cannot seem to write to the array "time" in the JObject.
I managed to write to the file but in root instead of inside the array.
I have tried json1.SelectToken("time") and lots of different approaches, like this one http://stackoverflow.com/questions/15413825/how-do-you-add-a-jtoken-to-an-jobject#15782238 and also some approaches from the Newtonsoft documentation.
Any help is appriciated
Problem solved by ((JArray)json1.GetValue("time")). Selecting the array in the JObject json1 and adding to that instead of the root.
Hope this will help someone.
((JArray)json1.GetValue("time")).Add(
new JObject(
new JProperty("name", textBoxName.Text),
new JProperty("id", textBoxId.Text),
new JProperty("visible", true)));
ti.WriteToJson(json1);

troubles in extracting data from facebook graph c#

i'm working on a program that exports all user public data but the graph is making troubles,i made an app and got all the permissions (the extended profile properties permissions, and extended permissions) to generate the access token which i use to extract data, what confuses me is that some profiles extract the hole data some other don't even if it appears in the section.
example:
attempting to export me/ exported all the data i have sports education hometown etc..
attempting to export friend/ exported public data + sports and no education even tho he got education in the about section and it appears to public or friends
attempting to extract me/subscribers showed 12 subscribers which is right
attempting to extract friend/subscribers showed 12 of 20 even tho the subscribers appears as 20 in the subscribers section
i'm using the following command:
var res = JsonConvert.DeserializeObject<FacebookLikes>(fb.Get(txtUserName.Text + "/likes").ToString());
i hope you understand the idea, thanks for the help
okay here the problem with a live test:
i tried the following on the tool you stated:
blazzzin/?access_token
the result was:
{
"id": "100001748944712",
"name": "Ethan 'blaze' Parker",
"first_name": "Ethan",
"middle_name": "'blaze'",
"last_name": "Parker",
"link": "https://www.facebook.com/blazzzin",
"birthday": "02/20",
"hometown": {
"id": "102161913158207",
"name": "Delhi, India"
},
"quotes": "You're what you eat. Really? I am an Apple then.",
"sports": [
{
"id": "102173226491776",
"name": "Soccer"
},
{
"id": "112285278784684",
"name": "Badminton"
}
],
"favorite_teams": [
{
"id": "138570342846531",
"name": "WWE Nexus"
},
{
"id": "136759993012177",
"name": "Delhi Daredevils"
},
{
"id": "42884080673",
"name": "Uruguay"
},
{
"id": "365383271987",
"name": "Chelsea FC"
},
{
"id": "151170071595882",
"name": "Best of wwe"
},
{
"id": "166496596733768",
"name": "WWE World"
},
{
"id": "19221964237",
"name": "ECW"
},
{
"id": "31695961025",
"name": "Impact Wrestling"
},
{
"id": "5985827589",
"name": "Fnatic"
},
{
"id": "61630257716",
"name": "University of Cincinnati Bearcats"
},
{
"id": "44027083759",
"name": "Cincinnati Reds"
},
{
"id": "323348924370835",
"name": "Wrestling Memes"
},
{
"id": "127573517418762",
"name": "Veer Marathi"
},
{
"id": "208056762558521",
"name": "Team Razer"
},
{
"id": "228215007216138",
"name": "The awkward moment when you go to grab someone sexy and headbutt the mirror"
},
{
"id": "200933243299323",
"name": "If she's never seen Arsenal win a trophy, she's too young for you Bro"
},
{
"id": "189411184456673",
"name": "Sanfransisco Giants"
},
{
"id": "210487655659656",
"name": "Team Bring It vs Team Cenation"
},
{
"id": "146995142049876",
"name": "Like IF the First THNG You Do WheN You WAKE UP is ROll OVR &CHeCK UR Phone"
},
{
"id": "111507918902981",
"name": "• WWE Universe | Tunisian Page •"
}
],
"favorite_athletes": [
{
"id": "104023396299513",
"name": "Andrew Flintoff"
},
{
"id": "545498258812287",
"name": "Luke Wright"
},
{
"id": "111832475503333",
"name": "Michael Schumaker"
},
{
"id": "110393655686342",
"name": "Eddie Alvarez MMA"
},
{
"id": "112941715451427",
"name": "Maryse"
},
{
"id": "344128252278047",
"name": "Sachin Tendulkar"
},
{
"id": "179559748752616",
"name": "Maryse"
},
{
"id": "52911737290",
"name": "Doraemon"
},
{
"id": "14320933255",
"name": "Andrew Flintoff"
},
{
"id": "81221197163",
"name": "Cristiano Ronaldo"
},
{
"id": "136644946409767",
"name": "Mahmoud's charity run from Melbourne to Sydney"
},
{
"id": "42888741032",
"name": "R-Truth - WWE Universe"
},
{
"id": "203402113048458",
"name": "Abby Marie Johnson WBFF Bikini 2014 Competitor"
},
{
"id": "270759951686",
"name": "JASON DAVID FRANK - Official Fan Page"
},
{
"id": "110336188978264",
"name": "AJStyles.Org"
},
{
"id": "8707340185",
"name": "Edge - WWE Universe"
},
{
"id": "298344150293189",
"name": "Fandango - WWE"
},
{
"id": "195569370462754",
"name": "William Regal: A Tribute To An Underrated Legend"
},
{
"id": "173741199368967",
"name": "Steve Moriarty"
},
{
"id": "12714756642",
"name": "Fedor Emelianenko"
},
{
"id": "212481352152206",
"name": "Arkan Taha Fitness"
},
{
"id": "160505357316902",
"name": "Lexi Thompson"
},
{
"id": "10035000964",
"name": "The Great Khali - WWE Universe"
},
{
"id": "176063032413299",
"name": "Leo Messi"
},
{
"id": "65920772679",
"name": "Maria Sharapova"
},
{
"id": "226790007363705",
"name": "Divas - WWE UNiVERSE"
},
{
"id": "175535596516",
"name": "Sabine Lisicki"
},
{
"id": "415063578542561",
"name": "Eddie Guerrero"
},
{
"id": "157829220895967",
"name": "Kaitlyn - WWE Universe"
},
{
"id": "122046337858201",
"name": "WWE Maryse Ouellet"
},
{
"id": "185950250927",
"name": "Brock Lesnar"
},
{
"id": "8164128018",
"name": "Layla - WWE Universe"
},
{
"id": "110108465743414",
"name": "PUSH EVAN BOURNE"
},
{
"id": "115647961808394",
"name": "We love Evan Bourne"
},
{
"id": "105683519512165",
"name": "Sin Cara - WWE"
},
{
"id": "125557244171559",
"name": "Rikishi"
},
{
"id": "8457822873",
"name": "CM Punk - WWE Universe"
}
],
"inspirational_people": [
{
"id": "17774451468",
"name": "Mr. Bean"
},
{
"id": "9972312428",
"name": "The Miz - WWE Universe"
},
{
"id": "103107176396108",
"name": "CM Punk"
},
{
"id": "10392229299",
"name": "Gurbaksh Chahal"
}
],
"education": [
{
"school": {
"id": "424030284317485",
"name": "HarvardX"
},
"year": {
"id": "120960561375312",
"name": "2013"
},
"concentration": [
{
"id": "108146682539885",
"name": "Justice"
}
],
"type": "College"
},
{
"school": {
"id": "102052863223529",
"name": "TGC Animation and Multimedia"
},
"year": {
"id": "120960561375312",
"name": "2013"
},
"concentration": [
{
"id": "199849176695930",
"name": "Web Design & Development"
},
{
"id": "109803049037749",
"name": "Graphic Design"
}
],
"type": "College"
}
],
"gender": "male",
"relationship_status": "In a relationship",
"significant_other": {
"name": "Hailey Hayden",
"id": "100006853479540"
},
"website": "http://www.varunpuri.me
",
"locale": "en_US",
"languages": [
{
"id": "106059522759137",
"name": "English"
},
{
"id": "113301478683221",
"name": "American English"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "110343528993409",
"name": "Spanish"
},
{
"id": "105606752807048",
"name": "Punjabi"
},
{
"id": "107672419256005",
"name": "Dutch"
}
],
"updated_time": "2014-01-05T16:49:55+0000",
"username": "blazzzin"
}
i tried it on another friend and the friend have all the data public example work and education (i can see them in the about section)
khalil.bsaibes/?access_token
the result was:
{
"id": "806750229",
"name": "Khalil G. Bsaibes",
"first_name": "Khalil",
"middle_name": "G.",
"last_name": "Bsaibes",
"link": "https://www.facebook.com/khalil.bsaibes",
"location": {
"id": "106188806084417",
"name": "Beirut, Lebanon"
},
"gender": "male",
"locale": "en_US",
"updated_time": "2014-01-12T09:39:29+0000",
"username": "khalil.bsaibes"
}
so can you explain me what's happening and how to solve it?
What if the user whose information you are trying to access hasn't made some of his information public?
You can use the below tool to debug by putting the access token and your query and seeing what is the returned json. If it returns only 12 subscribers, then you should check the permission, if it returns 20 then recheck your code and parsing method..
https://developers.facebook.com/tools/explorer/

Linq JObject query

I have some problems with jobject and jarray linq query. I get this error:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'.
My code:
string fetchResult = JsonConvert.SerializeObject(sidebar, Formatting.Indented);
JObject rss = JObject.Parse(fetchResult);
var jsonModel = from item in (JArray)rss["RegistrationCase"]
select new DataList
{
RegistrationTypeName = item["RegistrationTypeName"].Value<string>(), };
If i remove (Jarray) i get: Cannot access child value on Newtonsoft.Json.Linq.JProperty.
Json, jobject: e.g: i want RegistrationTypeName, FirstName and the value of JournalNumber.
{
"Status": null,
"RegistrationCase": {
"RegistrationTypeName": " ",
"ExpireDate": null,
"PersonId": 7,
"Person": {
"FirstName": " ",
"GenderValue": 2,
"Gender": 2,
},
"UserId": 7,
"User": {
"UserName": "NO-DOM\\wme",
"LastName": null,
"Id": 7,
},
"Transactions": [],
"Comments": [],
"CustomData": [
{
"Key": "JournalNumber",
"Value": "0654-84148-00000-25",
"Id": 3,
},
{
"Key": "IsConsentGiven",
"Value": "False",
"Id": 4,
},
{
],
"FileId": null,
"File": null,
"Id": 7,
}
}
u can get these values directly like :
var RegistrationTypeName = rss["RegistrationCase"]["RegistrationTypeName"];
var FirstName = rss["RegistrationCase"]["Person"]["FirstName"];
var JournalNumber = rss["RegistrationCase"]["CustomData"][0]["Value"];

Categories

Resources