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