Friday, June 26, 2015

Deserialize JSON Object in C#

As soon as I'm done with this the project is basically done. I mean, the important part is sending and receiving data through the Web Service using JSON. I can already receive it, but I still need to send it. In my past entry, I explained how I communicated with C# by Javascript. Now, I receive a big string which is a "stringified" JSON array. What I need to do deserialize that data so that I can correctly separate the data and then INSERT it to the database. This is the code I have right now:

    [WebMethod]
    public void SyncToServer(string D)
    {
        Console.Write(D);
        var jobs = JsonConvert.DeserializeObject<Job[]>(D);
        Console.WriteLine();
    }

The string D is the JSON I'm receiving. D has a string which is the JSON array. The class Job has the exact same fields that the JSON has, so it shouldn't be a problem. And that is the exact solution I've read in every place I've searched. But it's not working. The error I'm getting is the following:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Job' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I will continue to try and deserialize it.

UPDATE: I now copied the JSON array string to a variable and tried executing the same code, but no change. I suspect it has to be the way I'm doing it is incorrect. I still have to keep investigating.

UPDATE 2: I tried making the JSON in the variable a JSON object with the whole array as a parameter, but it's still not working.

UPDATE 3: Wow... The problem was the JSON. It was incorrectly formatted, it had a couple of extra brackets that didn't go there. That's why it wasn't recognizing the format. Now that I've managed to figure out where the flaw was, time to go back to the Javascript and find out where exactly is it going wrong in creating the JSON.

UPDATE 4: GOT IT! In the Javascript I changed the way I was writing the JSON in the localstorage. If the localstorage was empty, I pushed the object into an array then placed it on the localstorage. The problem was that it was inserting the whole object as an array, and I needed to insert just the object. Success!

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.