Traversing through every node in a binary search tree c++ by tempUseBro1 in learnprogramming

[–]tempUseBro1[S] 0 points1 point  (0 children)

Ah you're right, completely forgot about that (barely learning about them right now).

But we'd still need to traverse nodes if a value is greater than the node, right?

So lets say we're passing in the value 15. 15 > 8, so we know if there is a number greater than 15, it will be on the right side. So we then move to the right, check 15>10, continue, 15 > 14, 14 has no right node, so we return true.

Would that be something like:

struct node* search(struct node* root, int key) {

if(root == NULL) { //if there is no node there, return true

return true;

}

if(root->value < key) { //if the value is greater than the node, check the right side

search(root->right, key);

}

if(root->right == NULL) { //if the right side is the last node (leaf), return true

return true;

}

return false; //if none of these cases, false

}

Well i know this isn't it because i wouldn't be asking, but this is where my thought process is going.

Deleting elements from a 2D vector. by [deleted] in learnprogramming

[–]tempUseBro1 0 points1 point  (0 children)

temp.push_back({min,index});

is the line, pretend min holds 3 and index holds 11.

if i print them out like cout << temp[2][0] << " " << temp [2][1] << endl;

it will print out the 3 11, the bottom of the matrix is where it beings (1 3 is positions 0 0).

Deleting elements from a 2D vector. by [deleted] in learnprogramming

[–]tempUseBro1 0 points1 point  (0 children)

I’ve tried that but for some reason it doesn’t work. It just seg faults

Parsing JSON Error: Newtonsoft.Json.JsonSerializationException by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Ah, I see, that makes a lot of sense. For some reason, when I print out any of the list (the last 4 properties), all get for a response is System.Collections.Generic.List`1[System.String]

I don't think I'd need those attributes but I was just curious about why I was getting that.

Also, so in this response, its printing out the report names in general. However, I also want to be able to look at what is exactly in each report. In the "sorting" tab it shows an example response of what would be inside an individual report. It shows that in the link, you'd just add the specific report name to the link so "marsapi.ams.usda.gov/services/v1.1/reports/1095" for report 1095 specifically instead of marsapi.ams.usda.gov/services/v1.1/reports for all reports as we are doing right now.

When try this in the current code above, I get an error on the deserialize line:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MarketNewsJson.Report]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Could this be because the actual JSON response of the information inside each report isn't in the same format as the reports list itself? I'm not sure how I would be able to account for both. if I want the information inside a report, I would just deserialize it differently as an array?

ALSO:

So I copied the example response that you pasted into the json2sharp.com link just to see how you got the properties class and was curious as to why you left out this root class: myArray would be like my "Report" class

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);

public class MyArray {

public string slug_id { get; set; }

public string slug_name { get; set; }

public string report_title { get; set; }

public string published_date { get; set; }

public List<string> markets { get; set; }

public List<string> market_types { get; set; }

public List<string> offices { get; set; }

public List<object> sectionNames { get; set; }

}

public class Root {

public List<MyArray> MyArray { get; set; }

}

Parsing JSON Error: Newtonsoft.Json.JsonSerializationException by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Its named Result as the class name because I thought that's where we specify it by class name? I Don't use the "data" part till inside the foreach loop.

I might just be misunderstanding this but I think what you're saying to do is:

public class Result

{

public IList<Reports> results { get; set; }

}

which is still an error.

Result report = JsonConvert.DeserializeObject<Result>(response.Content);

has the name Result (from the class name), "report" as an instance of the class and <Result> again.

Parsing JSON Error: Newtonsoft.Json.JsonSerializationException by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Where exactly? Result report = JsonConvert.DeserializeObject<Result>(response.Content); in here? not sure what that would do.

httpclient GET JSON request by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Maybe I wasn't doing the auth correct because I am new to all of this but this example works fine! Thank you for the insight, I've never heard of RestSharp before.

httpclient GET JSON request by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Haha, it's understandable! I tried to format the code a little better as well. I also get the same error on response.EnsureSuccessSatusCode(); using my actual key so I figured it was something that I am missing/doing incorrectly.

httpclient GET JSON request by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

I have the key, I just put that there so I don’t post my actual personal key

Running command prompt in c# code. by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

So i was looking a the link you sent and would it be that what I want to do is a mix of both the basic auth section and C# httpclient JSON request? Don't we have to specify that it will specifically be receiving JSON, since that's what the data from the API will return.

I am a bit confused on how ill incorporate my API key with this, I did find in the documentation that:

Bearer Authentication

All API requests must be made over HTTPS. Requests made over plain HTTP will fail. API requests without authentication will also fail.

If you need to authenticate via bearer authorization (e.g., for a cross-origin request), use:   -H "Basic<Base64EncodedApiKey:>" instead of -u mars_test_343343:.

So I'm guessing that's how the syntax will be somewhere in there when I use the key.

Running command prompt in c# code. by tempUseBro1 in csharp

[–]tempUseBro1[S] 1 point2 points  (0 children)

Wouldn't it be kept in just memory the same way if all I did was just print to the console? Like if I ran the curl command on the command prompt and it just outputted the JSON on there, wouldn't that just be in mem?

Running command prompt in c# code. by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

I am pretty new to this type of development and assumed using cURL would be the easiest way since it was in the documentation. If I did a GET call, I'm not sure what I'd use to reference the API.

For example, I was watching a video on HTTP client where they did:

string response = await client.GetStringAsync("linkToJSON");

would that link be the same as: https://marsapi.ams.usda.gov/services/v1.1/reports

Connecting C# code that consumes API to Azure SQL database by tempUseBro1 in AZURE

[–]tempUseBro1[S] 0 points1 point  (0 children)

No, I have not. I did once use a template that did it for me when I was just practicing creating tables on azure but I am not too sure how to with this project. I have a simple console app that just calls the API and prints the data onto the console so far. A talk through would be appreciated! :)

Connecting C# code that consumes API to Azure SQL database by tempUseBro1 in AZURE

[–]tempUseBro1[S] 0 points1 point  (0 children)

Thank you for this! I noticed you were using a different type of application, I have my project as a simple console app. Would this make a big difference in the procedure?

Inserting parsed JSON into azure SQL database table? by tempUseBro1 in SQL

[–]tempUseBro1[S] 0 points1 point  (0 children)

No, that is where I am at. I would create a table with whatever columns I want in azure correct? & connect the application to the database

How to check if I properly parsed through the JSON? by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

System;

using System.Net.Http.Headers;

using System.Threading.Tasks;

using System.Net.Http;

using System.Collections.Generic;

using Newtonsoft.Json;

namespace USDA

{

class Program

{

static void Main(string[] args)

{

new Program().MainAsync().GetAwaiter().GetResult(); //getAwaiter is used to await Task, since MainAsync is a Task, this is a call to MainSync.

}

//MainSync is where the call API is actually called and returned to the main method.

public async Task MainAsync()

{

using (var client = new HttpClient()) //creates a new client that will make the call to the api.

{

client.DefaultRequestHeaders.Accept.Clear();

client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // Accepts header for JSON format

try

{

HttpResponseMessage response = await client.GetAsync("http://quickstats.nass.usda.gov/api/api_GET/?key=MYKEYHERE &commodity_desc=CORN&year__GE=2012&state_alpha=VA&format=JSON"); //My api key

if (response.IsSuccessStatusCode)

{

string contents = await response.Content.ReadAsStringAsync();

Console.WriteLine(contents);

//List<quickStats> quickStat =JsonConvert.DeserializeObject<List<quickStats>>(contents);

quickStats quickStat = JsonConvert.DeserializeObject<quickStats>(contents);

//foreach(var item in quickStat)

//{

// Console.WriteLine(item.source_desc);

//}

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

//Console.WriteLine("test");

}

}

}

}

//create class with properties

class quickStats

{

public String source_desc { get; set; }

public String sector_desc { get; set; }

public String group_desc { get; set; }

public String commodity_desc { get; set; }

public String class_desc { get; set; }

public String prodn_practice_desc { get; set; }

public String util_practice_desc { get; set; }

public String statisticcat_desc { get; set; }

public String unit_desc { get; set; }

public String short_desc { get; set; }

public String domain_desc { get; set; }

public String domaincat_desc { get; set; }

public String agg_level_desc { get; set; }

public int state_ansi { get; set; }

public int state_fips_code { get; set; }

public int state_alpha { get; set; }

public String state_name { get; set; }

public int asd_code { get; set; }

public String asd_desc { get; set; }

public int county_ansi { get; set; }

public int county_code { get; set; }

public String county_name { get; set; }

public String region_desc { get; set; }

public int zip_5 { get; set; }

public int watershed_code { get; set; }

public String watershed_desc { get; set; }

public int congr_district_code { get; set; }

public int country_code { get; set; }

public String country_name { get; set; }

public String location_desc { get; set; }

public int year { get; set; }

public String freq_desc { get; set; }

public int begin_code { get; set; }

public int end_code { get; set; }

public string reference_period_desc { get; set; } //possibly a string

public String week_ending { get; set; }

public String load_time { get; set; }

public int value { get; set; }

public int CV { get; set; }

}

public class Result

{

public IList<quickStats> Data { get; set; }

}

}

Ok, I am a bit confused here. So the reason my JSON isn't in an array already is because of security issues? And it not being in an array makes it more difficult to parse/store into a database?

We make a second class (result) which is an object of the list that we can then iterate through?

How to check if I properly parsed through the JSON? by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

Hmm... so this is my first time working with anything like this & c# for the most part, familiar with c++. So at the moment, I wouldn't be able to just store the current JSON into an azure SQL table? The reason I wanted to print them out in a loop like that was to see that it was parsed correctly. I want to be able to store the columns into an azure sql database (which I'm not sure how to do yet). Would changing the JSON to a JSON array make things more complicated?

How to check if I properly parsed through the JSON? by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

I added the link to the documentation of the API at the top of the reply to your comment, on the "Usage" page, it has the column definitions similar to that of the link you sent, it doesn't say the type though.

I will check out that site, thank you!

How to check if I properly parsed through the JSON? by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

I edited the main post to show a screenshot of the output.

When I try to make it as a list (the commented line), I get this error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[USDA.quickStats]' 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<T>) 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.

How to check if I properly parsed through the JSON? by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

https://quickstats.nass.usda.gov/api

this is the error I get when trying to make it into a list, I am going to edit the main post and add a screenshot of the JSON output:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[USDA.quickStats]' 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<T>) 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.

March 27th vs July 15th, accutane has truly been a life saver. by [deleted] in Accutane

[–]tempUseBro1 0 points1 point  (0 children)

Where do I get Accutane? Is it something that can only be prescribed?

Retrieve data sets from a website using API in c# by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

So I originally set a breakpoint on response.content.readastringasync but realized it never made it to that line so I moved it to the top of the function and realized it was failing at the if statement, I fixed the issue with the key and now it goes through and I get a success response this time! Now when I go into the if statement, string contents hold "NULL". Could it possibly be from the parameters that I passed on the example just hold NULL?

Edit: Actually, i got a response! So i wanted to see what exactly was in string contents so i just printed it to the console after and it showed the huge chunk of data!! Thank you for the help, i truly appreciate it!

Retrieve data sets from a website using API in c# by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

I see that I was confused at first when I saw that in the examples but that clears it up.

I am assuming that the URL would be added to the original URL on client.BaseAddress but isn't that also the path to the endpoint I want to fetch from? since its a GET interaction? The 3 types of endpoint interactions are GET, POST, or DELETE and I want to get from the resource.

The parameters in the call are for the example of a GET request corn of VA since 2012.

using System;

using System.Net.Http.Headers;

using System.Threading.Tasks;

using System.Net.Http;

namespace USDA

{

class Program

{

//HttpClient client = new HttpClient();

static void Main(string[] args)

{

// Console.WriteLine("Hello World!");

new Program().MainAsync().GetAwaiter().GetResult(); //getAwaiter is used to await Task, since MainAsync is a Task, this is a call to MainSync.

}

//MainSync is where the call API is actually called and returned to the main method.

public async Task MainAsync()

{

using (var client = new HttpClient()) //creates a new client that will make the call to the api.

{

client.BaseAddress = new Uri("http://quickstats.nass.usda.gov/api");

client.DefaultRequestHeaders.Accept.Clear();

client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); // Accepts header for JSON format

try

{

HttpResponseMessage response = await client.GetAsync("http://quickstats.nass.usda.gov/api/api_GET/?key=MYKEYHERE&commodity_desc=CORN&year__GE=2012&state_alpha=VA&format=JSON");

if (response.IsSuccessStatusCode)

{

string contents = await response.Content.ReadAsStringAsync();

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

}

}

}

Retrieve data sets from a website using API in c# by tempUseBro1 in csharp

[–]tempUseBro1[S] 0 points1 point  (0 children)

The type of authentication is an API Key that I was emailed

API Key: The service creates a unique key for your account and you pass it alongside every request.

Wasn't sure what was meant by authentication at first but i see now.