0

I have a web app that we're still stuck in asp.net 2.5 with. We've started using some newer technology with our front end, including loading some of our data into JSON objects and passing them around the application using localStorage. It's working really great.

In the future we're going to change our ASP.NET web form architecture into ah HTML5/JQuery front and Web API back end. So we're trying to write for that future while still being constrained to our old web form post backs and business objects. So right now we're posting from our search form to our search result page web form and we'll be calling a method from our business object to grab and return search results.

The criteria object we pass in has 20 or so values and a couple of collections (product line ID's, category ID's, etc..). So it's a slightly complicated object. In the old form we grabbed values from the controls, validated them, and passed them in using the asp.net controls, it was a single form solution. Our new solution has a search form and a results page. We're passing our values from form to form in a JSON object in internal storage. I can't really get to that from server side so I also stashed the values in a hidden field on the form that I can grab on the server side when I POST to the results page (eventually we'll call an API from the new form using ajax). So now that I can see the data, how do I parse and work with a JSON object in the code behind of asp.net. I need to load the 20 or so search criteria values and iterate through the ID collections (Guid and int) to load them into the same criteria object. This object is then passed in as the search methods parameter and search results will come back. Not sure how to manipulate the json on the server side.

2 Answers 2

3

If I understand the question, you have a JSON string in the server, and you just need to work with it.

Easiest way is to define a class (or in this case, a few classes) representing the data, then simply deserialize the JSON into an instance of the class. From there, you've got a regular object, and can do whatever you need with it, including serializing it back to JSON if you want.

With JSON.NET (aka Newtonsoft.Json), it's as simple as:

var myObject = JsonConvert.DeserializeObject<SomeType>(jsonString);

If you need help building the class, you can use json2csharp, where you can pass in a sample JSON file, and it builds the appropriate C# classes for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Newtonsoft.Json supports .NET 2.0, so looks like that would be a good place to start.
0

You can use DataContractJsonSerializer class, if the .Net framework is upgrade to 3.5 or later.

But, I found this article in web, http://www.netfxharmonics.com/2008/01/DojrNET-Dojo-RPC-Library-NET-20,

which is using Dojr.Net library and its compatible with .Net 2.0.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.