Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot convert object of type Json webMethod

Posted on 2015-01-19
6
Medium Priority
?
324 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
  • 3
  • 2
5 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

772 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