Solved

Jquery array receive deserializing error

Posted on 2014-07-23
13
208 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now