0

-JSON通过将请求从数组转换为远程 Windows Server 2008 R2 来发送请求,该远程 Windows Server 2008 R2 运行.NETWeb 服务。

-如果我的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,但仍然得到相同的响应...谁能帮我解决这个问题...非常感谢!

4

1 回答 1

0

您不能传递对象数组。您需要将数组包装在字典中并发送字典。就像是:

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

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

于 2013-05-17T06:20:02.630 回答