Solved

Cannot convert object of type Json webMethod

Posted on 2015-01-19
6
277 Views
Last Modified: 2015-01-20
using Newtonsoft.json
Everything says this should work.
[WebMethod]
         public static string AddClient(AllRequests[] AllResults)
         {
             string myself;
             foreach (var c in AllResults)
             {
                 myself = c.Company;
             }

Open in new window

{"AllResults":"[{\"Company\":\"testcompany\"}]"}

Open in new window


public class AllRequests
{
    public string Company { get; set; }
    
}

Open in new window

Error
{"Message":"Cannot convert object of type \u0027System.String\u0027 to type \u0027AllRequests[]\u0027","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object

Open in new window


var JSONObject = new Object; 
         JSONObject.AllResults = JSON.stringify(table);   
        JSONstring = JSON.stringify(JSONObject); 
alert(JSONstring);
alert(JSON.stringify(table));
   $.ajax({
                type: "POST",
                url: "/uest.aspx/AddClient",
                data:JSONstring,

Open in new window

0
Comment
Question by:sevensnake77
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:louisfr
ID: 40559522
This:
{"AllResults":"[{\"Company\":\"testcompany\"}]"}

Open in new window

is a JSon object with an AllResults property of type string and value "[{\"Company\":\"testcompany\"}]".
For it to be an array, you should not surround it with quotes:
{"AllResults":[{"Company":"testcompany"}]}

Open in new window

0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40559716
with quotes or back slash
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40559724
No quotes, and no backslash, around arrays. Only around strings.
{"AllResults":[{"Company":"testcompany"}]}
0
 
LVL 9

Author Comment

by:sevensnake77
ID: 40559731
I've requested that this question be closed as follows:

Accepted answer: 0 points for sevensnake77's comment #a40559716

for the following reason:

thanks
0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 40559732
great thanks it worked
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

19 Experts available now in Live!

Get 1:1 Help Now