Solved

Cannot convert object of type Json webMethod

Posted on 2015-01-19
6
271 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 32
c# code 19 59
Rolling back a sequence of stored procedures 12 27
Get list of word ducuments in a folder 10 17
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…
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 is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

706 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