What's the best way to parse JSON data into a .NET object? I am trying to assist a coder friend of mine and he is trying to dump some data from a JSON string into a database using ASP.net. Are there any prebuilt scripts that would make this happen?
Thanks in advance to any help.
The .NET Framework 3.5 has the JavaScriptSerializer class that can ease the deserialization. You can also use third party libraries like JSON.NET.
Actually you should really look at the DataContractJsonSerializer as the JavaScriptSerializer was listed as Obsolete in the .NET 3.5 framework.
Admittedly ScottGu stated that it may have been a mistake and it may be reinstated in the future.
If you are using .NET 3.5, you probably don't need a third party library. The JavaScriptSerializer class can be used (just repeating what was mentioned before) but you also have access to the DataContractJsonSerializer, which offers a different model for mapping between CLR objects and JSON.
Arguably the fastest way is to use JSON#, which avoids reflection and the associated performance overhead - this can be significant in web applications. It also gives you much more control in terms of the parsing process itself.
Related
Now i have to do some modifications in our old website which is developed in classic asp and vb script.but am not familiar with vb script.now i want to know how to use API's in classic with vb script and how read that xml output by using of vb script?
Your question is unfortunately a bit too vague to form a useful response. However, you should know that it is certainly possible to do.
Without any knowledge of the type of API you are discussing, let's just say in general terms that it is likely you will be utilizing the XMLHTTP object to create your 'request' to the API (though much here depends on the specifics of your API, whether it is SOAP, etc.), so you can start looking around for more information on that.
Retrieving and using the response will depend (again) on the specifics of your API. It is likely that you will be utilizing standard XML objects to iterate and retrieve the data you wish to work with.
For some interesting examples to help get you started, check out these:
http://benenewton.com/2009/09/02/calling-the-usps-api-via-classic-asp/
http://www.twilio.com/blog/2010/04/using-twilio-with-classic-asp-and-vbscript.html
http://classicaspreference.com/aspexamples/save_remote_images_with_photobucket_rss_xml.asp
I have been asking my self this question from the time i started workin in the software development field,
What is the best way to access data?
Which gives the best performance?
Which is the best for maintainability?
There are lots of solutions to deal with database in the Asp.Net web app,
Entity framework 4.0
Classes generator using ADO.Net such as Code Author ( i liked the way it works and the way it accesses the database using the data access block in Microsoft Enterprise Library).
i will start a new project tomorrow,and i don't know which approach is better??Any idea?
I'm using Castle Active Record for data access in our new ASP.NET project. It's a great and easy tool that is built upon NHibernate. NHibernate itself is a great ORM for .Net.
There is no "best". There is only preference. Some like MVC, some like WebForms, some like Dynamic Data. Entity Framework works nicely, as does LINQ to SQL(although I hear that one is being deprecated, but I'm fuzzy on that.) All work well.
Personally, I like WebForms, but if I want a quick and dirty CRUD app, I always opt for Dynamic Data, and if I need additional functionality mixed in, I can throw in some standard WebForm aspx pages.
Performance wise, I don't know that there's inherently a big difference. All use the same code under the hood. All are based on ADO.NET. Entity Framework and LINQ to SQL seem to have additional overhead compared to the old-fashioned WebForms, but proper DB design and planning is probably of greater importance.
Can anybody recommend the best way to do this?
Should I use JSON.NET?
You should definitely use JSON.NET. It's a great JSON serialization/deserialization library with huge community support and one of the best options performance-wise (even ASP.NET WebApi is using it as its default serializer).
You can also use .NET DataContractJsonSerializer or JavaScriptSerializer in case you can't or prefer not to reference an external library in your project for a simple task (if that's your case).
What are your best examples of using Reflection in production code?
ASP.NET MVC inferring the action and controller to invoke from URL. Routing in general.
Most recently, I had to use reflection to load an assembly (in order to investigate its public types) from just the path of the assembly.
Before Linq To Sql came out...I had to write my own ORM in .NET 2.0.
I used reflection heavily to reflect back on:
Classes to their CRUD stored procedures
Properties to their column names
Which operations were valid for the class
I also used reflection to handle all variable assignment after the results were retreived (all classes inherited an ActiveRecord class that handled the calls into the DAL).
Rough stuff...but after some performance tuning it wasn't half bad.
Pulling data out of an SQL table where you had an ID, a type, and other data
Then you could load "Chevrolet" and work with all of it's methods
I did have occasion to write a Python O/R mapper on one at one point, but it was a proof of concept and never went into production.
I do quite a lot of work that makes extensive use of the system data dictionary on a DBMS (for example a generic slowly-changing dimension loader). It might be argued that this is not dissimilar to reflective programming in principle.
Finally, Python in all its forms is very easy to do reflection with. In fact, it's so good at this that I've used it to poke about with underlying API's in other languages - and use the reflective capabilities to query the underlying interfaces. I have done this with pretty much every reflective mechanism that exists in the Python world: CPython on Python API's and COM API's using makepy, Jython for java API's and IronPython for .Net API's.
In one of my recent apps, an add-in for Kofax Express, I have an option to OCR a file and output a PDF. Since the OCR tool I'm using has a runtime fee, I made the OCR part a seperate assembly. If the file exists, I show the OCR options and late bind the assembly and invoke the required methods and attach to the events with reflection. A simple plug in architecture without interfaces, and saves customers from having to pay royalty fees if they don't need to OCR; we just don't give them the OCR dll.
WPF Databinding:
1) Binding path "(TextBox.Text)" vs "Text"?
If you bind to a path called Text, WPF uses reflection to resolve the name. If you use the class-qualified name, binding avoids the reflection performance hit. Class-qualified names also allows binding to attached properties!
(via http://dotnet.org.za/rudi/archive/2008/03/25/10-things-i-didn-t-know-about-wpf-data-binding.aspx)
NUnit Unit Testing Framework - Not very typical though
CSLA uses reflection a lot
Pretty much any Windows Forms app that supports plugins
My DAL is all reflection based. It reflects on the POCO properties to build SQL.
Within a factory, we use reflection to either pass back a "Dummy" implementation of an interface or a real (hooked to the DB) implementation of an interface, based on the class specified in a properties file (in Java).
I have a web application using ASP.NET 2.0 and I want to know if I should be moving it to ASP.NET 3.5, particularly... what am I missing by not moving to ASP.NET 3.5?
I understand the disadvantages, but I don't understand the advantages.
What are the biggest benefits of migrating/rewriting?
Will I get a speed improvement?
Is MVC that much easier than my old-fashioned WebForm application?
Will it look cooler?
You will only miss access to the newer .NET 3.5 libraries, and cool syntax such as LINQ and lambda expressions. Performance wise they will run the same.
By the way, ASP.NET MVC is NOT included with .NET 3.5...yet.
New C# 3.0 compiler features.
I'd say the biggest thing is Linq. At least it is for us, as we're completely replacing the old data layer with it! (Slowly, but surely.)
Yes, MVC is that much easier than your old-fashioned WebForm application.
So is LINQ to SQL.
There are also other MVC framework that works with .net2 (monorail, promesh,...), so mvc is not related to framework version, it is just a pattern.
But, new framework features that I use and find useful:
LINQ, LINQ2SQL
Extension methods
WCF services
WF
LINQ, dude. LINQ. Don't knock it 'till you've tried it. ORM is fun again!
LINQ, but not LINQ to SQL (which I don't really like). LINQ to XML and LINQ to Objects are fantastic.
Lambda expressions FTW! Linq's extension methods for collections combined with lambda expressions are awesome.
Nobody has mentioned Extension methods yet?!? See http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-extension-methods.aspx
And the items above (especially LINQ, Lambda expression, object, collection, and property initializers, etc.).