SQL data to JSON file - c#

I have a question for you stackoverflow experts :). I'm planning to use the FLOT jQuery plugin to chart some data to the MVC3 application I'm building.
I have several views that return data, but I'm just using a resultset and not an entity framework of any sort (it's nice and fast). I've never worked with JSON before, and I have the following questions:
a) Do I have to use a data model to return data to the controller, then use the JSON method to create the JSON file, or can I return the data without using an entity framework and achieve the same result?
b) Is there a bit of sample code anywhere that can help me out with this (or a link to a good example?
Many thanks in advance ;-).

If you can get your data into a class with properties (get;set;) then you can simply use the built in serializer for MVC ie return Json(yourClass);
If you want a bit more control over it, check out
http://json.codeplex.com/
Theres a linq-to-json and a dataset converter as well.

Related

Simple data collection application using JSON and .NET

I'm building a web application that consists only of one page (Literally one page, no HTML generation). The whole purpose of this application is to collect data from users and then save it into a SQL Server database.
I want to use Javascript to validate and collect the data from the HTML Form, put it in a JSON object, and then use C# and .NET to insert the data into SQL Server.
I have no clear idea on how to achieve this, and honestly, I'm not really sure if this is a valid model or not!
Any pointers or ideas on what to look for and where to start? And how can I achieve this in the simplest way possible?
I would suggest using jQuery and ASP.NET MVC. Here is an example:
Posting simple json to MVC action controller method with jquery
or this one:
Send data to MVC controller using JSON

Accessing JSON on server side (asp.net)

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.
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.
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.

How can I seralilze a value in C# to store in a mySql database that WordPress can deserialize?

I'm building an app that updates a WordPress database; WordPress stores serialized values in some of its fields. I need to serialize values and store them in WordPress so that WordPress can still deserialize them as normal, as if the data was entered directly in WordPress.
Does anyone know how to do this?
Here's an example of a value stores as serialized data in mySql:
_voucherLocations = a:5:{i:0;s:3:"All";i:1;s:0:"";i:2;s:0:"";i:3;s:0:"";i:4;s:0:"";}
All I entered in the WordPress form was "All"
When someone enters "All" in my WinForms form, it has to be stored the same way as seen above in the mySql database so the theme I'm using in WordPress recognizes it and can use it. Hope this is clear.
I'm using C# with Entity Framework 5 and the latest mySQL Data Adapater provided by Microsoft in a WinForms app. BTW if anyone does this and gets exceptions when trying to do any insert, update or delete operations, it's because Entity Framework changes the identity integer in mySQL to an unasigned decimal; I had to manually go in the entire Entity Framework generated code and change all those wrong data declarations into bigint from unsigned decimal. Just a heads up for anyone trying it.
PHP has special function to serialize and deserialize data, it is not JSON. I know only one library which can serialize and deserialize this data in .NET: csphpserial
I would make use of one of the JSON libraries for this. A popular one that ships with ASP.NET MVC is Json.NET and is available as a NuGet packe for install as below:
PM> Install-Package Newtonsoft.Json
You may also try another library which is gaining popularity, ServiceStack Text:
PM> Install-Package ServiceStack.Text
Looks like it's just JSON serialization. You should be able to do that easily in C#.
Have a look at JSON.NET

Alternative to returning a DataTable from a WCF Data Service

I have a reporting application that interfaces to a users system and returns data to be used in reports. As I have no control over he data and no knowledge of the data structure I have been using C# DataTables to hold the Data.
A requirement has no arisen to expose the data my application returns through PowerPivot via a webservice, now I understand that I need to use a WCF Data Service to achieve this.
My problem is I don't want to return a DataTable object, I would rather convert it to something more manageable and return that.
Can anybody suggest alternatives to do this?
I would suggest you to use Xml Serialization over the data table which is more manageable and also a good practice.
Have a look at the DataTableSurrogate, it is used in the SyncFramework for serializing DataTables.
MSDN Data Table Surrogate

JSON-friendly database?

I have a pure jQuery app that keeps it's objects in JSON format and provides client-side editing. I want to persist the JSON to a database, but everything seems like overkill (like object mapping in C#). I am close to settling on storing the whole JSON string into a single column of a database. I was hoping there was more of an elegant, creative approach like a jQuery/JSON/database framework out there, but couldn't find anything (maybe NoSql can be of use?).
Any suggestions or advise on how to persisting JSON to a database using as less server code as possible?
I would check out MongoDB due to it's JSON style documents.
It also has plenty of drivers, so I wouldn't see it being an issue.
I assume it's ok to use a server side technology, your description wasn't real clear for me.
The first thing that comes to mind for your needs is MongoDB (http://www.mongodb.org/). It sports a javascript shell and objects that are natively represented in JSON (BSON technically).
The server supports indexing and other things that give you the warm SQL fuzzies.
What about CouchDB? It uses JSON notation to store the objects and you could access the db directly from the client side via its REST interface.
Besides, there are already several jquery plugins for CouchDB, jqCouch just to mention one.

Categories

Resources