Solved

Jquery array receive deserializing error

Posted on 2014-07-23
13
209 Views
Last Modified: 2014-07-24
hi, well i am pretty familar with Serialization but not for c# for deserializing.
from my link to jfiddle I am passing the values to the server side.


On the server side I am trying  to take the values and submit them to a server

 public static string CheckExists(string Threshold, int sec) //Method should be declared as static 
        {


            DataContext db = new DataContext();
            Clear aTransaction = new Clear();
            List<IaTransaction> Threshold = JsonConvert.DeserializeObject<List<aTransaction>>();
            try
            {
               foreach  (var grds in Threshold)
{
              
                aTransaction.APTransactionSecurityTypeID = sec;
                aTransaction.Threshold =  sec;
                db.ClientPreClears.InsertOnSubmit(aTransaction);
                db.SubmitChanges();
}
            }
            catch (Exception ex)
            {
            }
         
            return JsonConvert.SerializeObject(Threshold);

        }

Open in new window


Not totally sure of the syntax but it would be so appreciated.
0
Comment
Question by:sevensnake77
  • 8
  • 4
13 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 167 total points
ID: 40215672
Are you trying to create a web method that takes your JSON object as a whole or as separate parameters (Threshold and sec)?
It seems that C# code above does not compile because Threshold is changed from string to a generic list. (line 7). Is the list the one required to convert to JSON and return it back to the AJAX call?
What is IaTransaction class declaration?

If you could be more specific, the JS ajax code will call the server method and what kind of output you are expecting back from the server method.
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215811
Well there is no need to return anything I just want to pass the json array objects as a method. I want to loop through the array and send it to the server to insert into a table.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40215871
What kind of web project is this? MVC? Web Forms?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Author Comment

by:sevensnake77
ID: 40215892
web Forms 3.5 frameworks
asp.net
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
ID: 40215898
How does this function get invoked by the browser? There must be another method that calls this method. What does that method look like?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215904
the button like in the example but it will be a button control.<asp:button runat=server id=btn />
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215907
if you look at the jfiddle example it will invoke on post.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40215909
OK, but how are you getting the data from the AJAX POST into that method (on the server)?
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215936
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215949
data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]";


ar values = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(data);

Open in new window


Ok since I am getting a lot of questions with questions. I have a simple question. see where it says data in my jfiddle code how can I get data to look like the above when sending my data.  data is called when Deserializobject.
string>[]>(data);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 333 total points
ID: 40215961
You're getting questions because it's not clear how you are getting the data into your code. Regardless, to answer your latest question, you need to have a data structure that mimics the structure of your JSON:

public class Data
{
    public int ShiftID { get; set; }
    public string EmpName { get; set; }
}

Open in new window


That would take care of the innermost objects, but your root data structure is an array. So you are attempting to deserialize an array of this new data structure above.

e.g.

var data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]";
var values = JsonConvert.DeserializeObject<Data[]>(data);

Open in new window

0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40215980
But I use the jfiddle example to show how I am getting data. if you just try the jfiddle example it will do a popup showing you the objects. passing to a var data it my biggest problem.
http://jsfiddle.net/Nbr7Y/6/

now you have var data with the values static, how can I get my json data to the server side
Instead of
var data = @"[{""ShiftID"":""2"",""EmpName"":""dsdsfs""},{""ShiftID"":""4"",""EmpName"":""dsdsfd""}]";

Open in new window

I want {"Threshold":["343","32","2","1","3"],"sec":["3","32","2","1","3"]}
how can I get each object from this example
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 40217306
i figured it out.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question