Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cannot convert object of type Json webMethod

Posted on 2015-01-19
6
Medium Priority
?
309 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:Seven price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Seven price
ID: 40559716
with quotes or back slash
0
 
LVL 11

Accepted Solution

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

Author Comment

by:Seven price
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:Seven price
ID: 40559732
great thanks it worked
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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