Solved

Cannot convert object of type Json webMethod

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

630 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