Solved

Jquery array receive deserializing error

Posted on 2014-07-23
13
207 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 74

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 74

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 9

Author Comment

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

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

16 Experts available now in Live!

Get 1:1 Help Now