I've have a site which calls into a service and retrieves data. This data would then be bound to controls on the page immediately after retrieving the data (i.e. not stored locally).
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.
Is there another library I could utilise or any examples I could follow? Another question is if the service returning the data used is similar to the URLs:
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?
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.
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
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
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.
EssCee
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.
Fair point I was basing it on the JSON response but I assume I would be able to do something like product.CategoryName once I have the class working.
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
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.
Ok I just skimmed through the documentation and it doesn't support .Net 4.0 which is what this project is based on, so to keep it safe I'll have to stick with HttpWebRequest but try this on a test project.
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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.
IMHO :
- 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
@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.
Member
288 Points
1286 Posts
JSON serialiser and deserialiser
Nov 21, 2019 07:20 PM|uid250511|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.
Is there another library I could utilise or any examples I could follow? Another question is if the service returning the data used is similar to the URLs:
HTTPS://site.com/API/GetCategories
HTTPS://site.com/API/GetProducts
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?
All-Star
53711 Points
24031 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 08:31 PM|mgebhard|LINK
Web API handles JSON serialization automatically. Can you explain the problem you are trying to solve?
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.
Member
288 Points
1286 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 09:28 PM|uid250511|LINK
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
All-Star
53711 Points
24031 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 09:42 PM|mgebhard|LINK
HttpClient is a better choice and has async deserialize capabilities.
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.
Member
288 Points
1286 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 09:51 PM|uid250511|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
All-Star
53711 Points
24031 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 09:55 PM|mgebhard|LINK
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.
Member
288 Points
1286 Posts
Re: JSON serialiser and deserialiser
Nov 21, 2019 10:01 PM|uid250511|LINK
Ok I just skimmed through the documentation and it doesn't support .Net 4.0 which is what this project is based on, so to keep it safe I'll have to stick with HttpWebRequest but try this on a test project.
Do you know if this is this of an MVC course?
Contributor
4040 Points
1568 Posts
Re: JSON serialiser and deserialiser
Nov 22, 2019 09:15 AM|yij sun|LINK
Hi EssCee,
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.
E.g:
Notice:.NET Framework's verson is 4.5 or later.
More details ,you could refer to below article:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
Best regards,
Yijing Sun
All-Star
48720 Points
18184 Posts
Re: JSON serialiser and deserialiser
Nov 22, 2019 09:54 AM|PatriceSc|LINK
Hi,
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.
IMHO :
- 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
Member
288 Points
1286 Posts
Re: JSON serialiser and deserialiser
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.