2

I am a newbie to programming, so please bear with me.

Background

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

  2. 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!).

  3. 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});


});
  1. 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:

  1. This is great! But how can I 'decode' this data within the web method in C# so that I can access each piece of data to eventually write a record to a SQL database for each Note 'object'.

I hope this makes sense. Thank you in advance.

2

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ver much for your quick reply. I shall take a look this weekend.
Eureka! I think I have it. I shall post the working solution soon...phew, feel good.
1

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/

2 Comments

Cheers for your quick reply. I shall take a look this weekend and let you know how I get on.
Thanks Rajeev, I think I have it and your post most definitely helped with the json documentation.
0

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.

2 Comments

Thanks for your very quick reply and references, I will let you know how I get on!
...still working on this! I realised I needed to get back to basics with C# before I tackled this again.

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.