Last post Nov 22, 2019 03:23 PM by uid250511
Nov 21, 2019 07:20 PM|uid250511|LINK
Nov 21, 2019 08:31 PM|mgebhard|LINK
I converted the JSON into a class but I'm looking for a way to serialise and deserialise the data so I can use the class to hold the data. I read on JsonConvert but also read it had issues or not as flexible.
Web API handles JSON serialization automatically. Can you explain the problem you are trying to solve?
Would I call both URLs if I want to display products with the category text? So assuming the products have a category id, how could I display the text?
The design is totally up to you. There is nothing stopping you from returning a Product object that has a Category property or a Category with a list of Products. There also is nothing stopping you from creating a new controller that returns this type
of information or even an Action.
Nov 21, 2019 09:28 PM|uid250511|LINK
Nov 21, 2019 09:42 PM|mgebhard|LINK
I'll be using HttpWebRequest to do this not WebApi. The issue I had earlier was the JSON returned would not parse into the object type I passed in. This led me to search on the error where it felt there were better ways to do this but I just wasn't aware of
which is recommended i.e. Newtonsoft (or similar) but I didn't get a chance to read into this
HttpClient is a better choice and has async deserialize capabilities.
As for the second issue, if I have a product listed with category id 1, I don't want to display 1 as the category but rather the text id 1 it's associated with. For that reason would i just call the GetCategories method again or is there another "linked" way
to get the text without having to make a second call. When I did something similar for XML I added a property to the class along with the binding name which resolved this but I couldn't test this due to the first issue above. Hope this makes sense
You are making up restrictions where there are none. Design an object model that fits the design requirements and fill the object model with whatever data you like.
Nov 21, 2019 09:51 PM|uid250511|LINK
Nov 21, 2019 09:55 PM|mgebhard|LINK
I've never used HttpClient. Would this be a simple enough learning curve as I'm using an web forms? On a slight tight deadline ☹️. If no I may have to check out JsonConvert
HttpClient is similar to HttpWebRequest. They both do HTTP requests and handle the response. HttpClient is easier to use IMHO. I have no idea if you'll have the same "ease of use" experience.
Nov 21, 2019 10:01 PM|uid250511|LINK
Nov 22, 2019 09:15 AM|yij sun|LINK
If you want to use httpclient in the test proect, I suggest you could try to refer to below [article](https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client).
You could use httpclient PostAsJsonAsync or other method to post the request to the serverside.
static async Task<Uri> CreateProductAsync(Product product)
HttpResponseMessage response = await client.PostAsJsonAsync(
// return URI of the created resource.
Notice:.NET Framework's verson is 4.5 or later.
More details ,you could refer to below article:
Nov 22, 2019 09:54 AM|PatriceSc|LINK
Newtonsoft is widely used and IMO you likely ditched it too soon based on assumptions :
- seems you read something telling you may have a "better" solution but it doesn't give an alternate? Are you sure it applies to your own case which seems simple enough ?
- then you had a problem but it's unclear if you found a real issue with this library or if it could be easily fixed (seems you want to lookup a value based on text and to depend rather on the api side design ?)
It is also unclear if you try to consume your own service or a 3rd party.
- when reading keep in mind there is no such thing as the "best" option. It always depends on your own needs and constraints.
- when something doesn't work understand why rather than just trying something else (at least too soon and maybe for no real reason)
- you can always do something simple and if well designed enough that you could move forward later rather looking for a better library from which you'll perhaps use anyway only few capabilities
Nov 22, 2019 03:23 PM|uid250511|LINK
@PatriceSc - I decided to go for Newtonsoft this morning when i was more fresh and then queried the data and eventually had it working.
One of my concerns was performance as once i retrieve the data i need to bind it to the control (without storing the data locally as its a third party service). Eventually i thought no matter how i get the data if im not storing the data locally and simply
referencing it it will have the same problem.