0

- I am sending a JSON request by converting it from an array, to a remote Windows Server 2008 R2, running a .NET webservice.

- If my JSON request is successfully executed then i will be replied with a OK string.

- But i am receving System.InvalidOperationException.

** Array string:**

             (
        {
        add = "1 Stockton St";
        ccode = US;
        city = "San Francisco";
        country = "United States";
        cross = "at Ellis St";
        dist = 8;
        img = "https://foursquare.com/v/apple-store/42cc7080f964a520e9251fe3";
        lang = "-122.4064";
        lat = "37.78584";
        pcode = 94108;
        state = CA;
        vid = 42cc7080f964a520e9251fe3;
    }
)

Converted JSON String:

[{"city":"San Francisco","add":"1 Stockton St","ccode":"US","vid":"42cc7080f964a520e9251fe3","img":"https://foursquare.com/v/apple-store/42cc7080f964a520e9251fe3","state":"CA","lat":37.78584,"lang":-122.4064,"dist":8,"pcode":"94108","cross":"at Ellis St","country":"United States"}]

Exception thrown from the webserver:

{"Message":"Type \u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\u0027 is not supported for deserialization of an array.","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

Code for sending:

NSString* jsonString = [newArr JSONRepresentation];

    SBJSON *json = [SBJSON new];
    json.humanReadable = YES;
    responseData = [[NSMutableData data] retain];

    NSError *respError = nil;

    NSString *service = @"/DerializeDataTable";

    NSString *requestString = [NSString stringWithFormat:@"%@",jsonString];

    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

    NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
    NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
    NSString *urlLoc = [fileContents objectForKey:@"URL"];
    urlLoc = [urlLoc stringByAppendingString:service];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlLoc]];
    NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
    [request setHTTPMethod: @"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody: requestData];    

    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];

    if (respError)
    {
          //error in request
    }
    else
    {
        NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];

        NSDictionary *results = [[responseString JSONValue] retain];

    }

I am not able to detect the problem, i have even tried using SOAP, but still i got the same response... Can anyone please help me out of this... Thank you very much !!

Rohan
  • 2,939
  • 5
  • 36
  • 65
  • may be this que: http://stackoverflow.com/questions/2067742/system-invalidoperationexception-request-format-is-invalid-multipart-form-dat helps – Nitin Gohel May 17 '13 at 05:53
  • 1
    This is a server side problem. It is trying to turn a dictionary into an array for some reason. – borrrden May 17 '13 at 05:56
  • @borrrden - Thanks for reply. The Server is using the following code: **[WebMethod] public string DerializeDataTable(string n) { var table = JsonConvert.DeserializeObject(n); return “OK”; }** – Rohan May 17 '13 at 06:10
  • You need to say what DataTable is. Also, you need to debug on the server side, not the client side. This question needs to be tagged differently. The client side is fine. – borrrden May 17 '13 at 07:20
  • This [link](http://stackoverflow.com/questions/12397733/deserializing-a-list-of-objects-that-contain-a-dictionary) can be helpful – Amar May 17 '13 at 07:38
  • @borrrden - Thanks again. The problem is that when no any DataTable is used and just trying to return the data passed from the client app, it still gives the same error. – Rohan May 17 '13 at 08:48
  • You are using it in that code. The JsonConvert is trying to deserialize the string into a datatable (That is what the type argument is for) – borrrden May 17 '13 at 08:53
  • @borrrden- Ya you are right. But when I tried like - **[WebMethod] public string DerializeDataTable(string n) { return n; }**. It gives the same error – Rohan May 17 '13 at 09:39
  • Your method is error-prone and not generic. Check this method [here](http://stackoverflow.com/questions/14958883/ios-serialize-deserialize-complex-json-generically-from-nsobject-class/16771574#16771574). It is inspired by .NET [json.codeplex](http://json.codeplex.com/). Now in iOS and .NET you have both generic, less error-prone, beautiful and maintainable code – Alphapico May 28 '13 at 09:55

1 Answers1

0

You can't pass an array of objects. You need to wrap your array inside a dictionary and send the dictionary instead. Something like:

NSString* jsonString = [@{@"data" : newArr} JSONRepresentation];

With associated changes on the server as required for the data structure it expects to handle.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • can you elaborate why? that looks like valid JSON - is it a limitation of JsonConvert .NET API? – maroux May 17 '13 at 06:25
  • Yes, it's an issue on the server side. – Wain May 17 '13 at 06:29
  • @Wain - Thanks for replay. I have tried your code but it gives an error - **ExceptionType = "System.InvalidOperationException"; Message = "Type 'System.String' is not supported for deserialization of an array.";** – Rohan May 17 '13 at 06:35
  • I don't know .net, did you change the server code to expect a dictionary? – Wain May 17 '13 at 06:51