Thursday, June 25, 2015

Sending JSON to C#

I'm trying to POST a JSON from Javascript to a C# asmx. Apparently there is an error trying to deserialize the JSON. I've already tried creating a class with the same parameters, but somehow it didn't work. I also tried deserializing the JSON to a string List, but to no avail. The JSON is an array, and it comes in string format. I'm trying to get into it. We'll see how it goes.

UPDATE: So the error I was getting has changed. Originally I had this:

Type 'System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array

Now, I'm getting this:

  1. Invalid web service call, missing value for parameter: 'job'."
  2. StackTrace" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters) ↵ at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters) ↵ at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams) ↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"

I would like to thank the following link for providing some light to my problem: http://blog.dj-djl.com/2013/05/type-systemcollectionsgenericidictionar.html

Now, according to that link, one of the problems I had was placing the JSON Array in a simple JSON object. So what I'm assuming is that you can't send it as an array, but you can send it as JSON Object and make the array a parameter. I'm not sure if that makes sense as I wrote it.

As I wrote this, I realize that the class I'm using to get the JSON is not declared as an array. This could be why I'm getting that error. I will verify and update.

UPDATE 2: Changed it to array but it didn't work. I'm going to keep verifying some things but I might just try to use a string list instead of the object I created for the JSON to get the data. Be back in a few.

UPDATE 3: Changed the name of the object to match the name of the parameter in the JSON and to no avail. BUT

  1. Message"Cannot convert object of type 'System.String' to type 'Job[]'"
  2. StackTrace" at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) ↵ at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) ↵ at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams) ↵ at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters) ↵ at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams) ↵ at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)"

The error message changed. It can't be converted to the type of object I defined. Why? I'm not completely sure, but I have a feeling it's because the array of data that I need is inside a parameter.

UPDATE 4: Changed the object to a string array and I still get an error.

UPDATE 5: FUCKING GOT IT!!!!! Ok so what happened was that when I placed the JSON Array inside a JSON parameter, I no longer needed to get a string array, but rather one string which is all of the array. Now I get all of the array in string format. What follows is parsing that string to get the information I need. For now I'm ending it here.

No comments:

Post a Comment