Reputation: 73
I am a newbie to programming, so please bear with me.
Background
I have created a jQuery function that picks up the text, top and left coordinates for various text boxes on my page and I wish to pass all this information using an AJAX POST to a web service written in C#.
I have been successful in passing this data for one textbox to the web service method and inserted a record into a SQL database (I won't go into how long this has taken me!).
In order to write data for multiple text boxes, I am using a jQuery function containing an array of objects, for which I have taken inspiration from the first option in the following post: Jquery multidimensional arrays
Here's my code:
function Note(noteText, topCoord, leftCoord) {
return {
noteText: noteText,
topCoord: topCoord,
leftCoord: leftCoord
}
var noteData = [];
function SaveNote() {
{'input').filter("notes").each(function(index) {
var noteText = ($this)).val();
var coord = ($this)).offset();
var topCoord = coord.top;
var leftCoord = coord.left;
noteData.push(Note(noteText,topCoord,leftCoord));
var jsonText = JSON.stringify({ noteData : noteData});
});
Alerting variable jsonText I receive the following:
{"noteData":[{"noteText":"This is note text" ; "topCoord":23.33 ; "leftCoord":12.23}, {"noteText":"Note text 2" ; "topCoord":23.33 ; "leftCoord":12.23}]}
The Problem:
I hope this makes sense. Thank you in advance.
Upvotes: 2
Views: 3754
Reputation: 91497
Create a class that you can deserialize to:
public class Note()
{
public string noteText { get; set; }
public float topCoord { get; set; }
public float leftCoord { get; set; }
}
Then Deserialize it using a JavaScriptSerializer:
var jsSer = new JavaScriptSerializer();
Note note = jsSer.Deserialize(json);
Upvotes: 3
Reputation: 4992
The parameter will arrive to your method as an array of dictionaries. Utilitze the following prototype for your WebMethod
(if this is inside a web service rather than a page method, remove the static
identifier):
[WebMethod]
public static void MyMethod(Dictionary<string, object>[] noteData)
From here, you can iterate over every array member and access the elements as needed:
foreach (Dictionary<string, object> note in noteData) {
DataObject myDataObject = new DataObject();
myDataObject.noteText = note["noteText"];
...
}
Check this useful article from Encosia for some general info on how to utilize jQuery to call your web service with this information if you're not used to getting data from your JavaScript into your web service.
Upvotes: 0
Reputation: 932
One option would be to use the Json.net library in your web service. Its an open source library and can be downloaded from http://json.codeplex.com/
Using the library you an convert your Json string to Xml with a single line of code:
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
Once you have it as XML, it should be easy to save to the database. Or, if you prefer, you can create a .net object to represent the Json data and deserialize the Json text to the .net object using the JsonSerializer class from the library.
The documentation for the Json.net library can be found at http://james.newtonking.com/projects/json/help/
Upvotes: 1