1

I Have a json array like

[{"name":"A","value":"A"},{"name":"B","value":"B"},{"name":"C","value":"C"}]

How can i pass(to the backend) and store(in db) that javascript object with asp.net??

EDIT:

OK to make it clearer, in some page, i got a javascript object

var array = [{"name":"A","value":"A"},{"name":"B","value":"B"},{"name":"C","value":"C"}];

, how can i pass it to some handler (like using

`$.post({url:"handler.ashx", data:array}) 

??), then in backend how can i save it into database or load this array later??

OR:

I'll appreciate it if u teach me how to convert

var array = [{"name":"A","value":"A"},{"name":"B","value":"B"},{"name":"C","value":"C"}];

into:

var arraystring = '[{"name":"A","value":"A"},{"name":"B","value":"B"},{"name":"C","value":"C"}]';

EDIT2:

Now i use the string-solution (traversal the array and add some '"' ':' ',' '{}' manually) as i mentioned just above, is there any potential problems??

6
  • how do you want to store it? As a JSON string, as separate records? If you could also show the relevant target db schema that would help
    – Russ Cam
    Commented Aug 30, 2011 at 12:02
  • You are asking a very broad, abstract question. 1) How to do ajax, 2) how to convert JSON to .net classes/objects; 3) how to store objects in a database. You might as well be asking "how do I create a web application." Try being a little more specific. What have you done so far? What technologies are you using - database, jQuery, etc. Commented Aug 30, 2011 at 12:18
  • ASP.NET Webforms or MVC?
    – balexandre
    Commented Aug 30, 2011 at 12:18
  • @balexandre - I assume Web Forms since he mentioned the use of a Handler.
    – Phill
    Commented Aug 31, 2011 at 2:47
  • @balexandre Does Webforms or MVC really matter??
    – rhapsodyn
    Commented Aug 31, 2011 at 6:16

4 Answers 4

1

You have several options when dealing with JSON on .NET

  • You can simply keep the array as is in the DB and retrieve it as is and use it
  • You can parse it into an Object using JSON.NET Library

for example on the 2nd case you simple use the JSON.NET method:

Newtonsoft.Json.Converters.KeyValuePairConverter

Or be fancy and use a Custom Deserialize as it's really easy to pass to and from JSON.


Let's imagine that you are posting something like this:

$.post("myForm.aspx",
       [{"name":"A","value":"A"},{"name":"B","value":"B"},{"name":"C","value":"C"}],
       function() {
          // all done.
       });

This will be converted to A,B,C when requesting the data using Request.Form["name"] or simply Request["name"] so you should change all the naming convention first and then you can simple use Request["nameA"], Request["nameB"], etc...

2
  • 1
    There's also at least two framework JSON serializers that work just fine for most purposes, JavascriptSerializer and DataContractJSONSerializer. Nothing against the Newtonsoft one, I've used it and its great, but its also an unneeded dependency for many purposes. Commented Aug 30, 2011 at 12:16
  • I used Newtonsoft since the beginning when nothing more was that good, that's why I'm to dependent on it :)
    – balexandre
    Commented Aug 30, 2011 at 12:17
0

You would store it just like any other string you wanted to store in the database. If you want to re-instantiate it later in JavaScript you use the eval() function on the string to convert it back into an object.

1
  • But now, it's not a string, isn't it? And how can i pass it to the backend? When i use $.post, it can't work.
    – rhapsodyn
    Commented Aug 30, 2011 at 12:11
0

The best way to do this is to de-serialize the json into object and save it to db as records.

Check JSON.NET library http://json.codeplex.com/

public class MyObj
{
    public string name{get;set;}
    public string value{get;set;}
}

void Main()
{
    string json ="[{\"name\":\"A\",\"value\":\"A\"},{\"name\":\"B\",\"value\":\"B\"},{\"name\":\"C\",\"value\":\"C\"}]";
    IList<MyObj> arr = JsonConvert.DeserializeObject<IList<MyObj>>(json);

    foreach(MyObj o in arr)
    {
        //add each object to db...
        Console.WriteLine(o.name+":"+o.value);
    }
}
0

Ok it turns out that using JSON.parse and JSON.stringify is a not-bad solution.

Not the answer you're looking for? Browse other questions tagged or ask your own question.