JSON请求返回异常

时间:2013-05-17 05:48:42

标签: iphone .net ios objective-c json

- 我发送JSON请求,方法是将其从阵列转换为运行.NET网络服务的远程Windows Server 2008 R2。

- 如果我的JSON请求成功执行,那么我将回复OK字符串。

- 但我正在接受System.InvalidOperationException

**数组字符串:**

             (
        {
        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;
    }
)

已转换的JSON字符串:

[{"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"}]

从网络服务器抛出异常:

{"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"}

发送代码:

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];

    }

我无法检测到问题,我甚至尝试过使用SOAP,但我仍然得到了相同的回复......任何人都可以帮助我解决这个问题...非常感谢!!

1 个答案:

答案 0 :(得分:0)

您无法传递对象数组。您需要将数组包装在字典中并发送字典。类似的东西:

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

根据需要处理的数据结构,在服务器上进行相关更改。