【问题标题】:Request of JSON returns ExceptionsJSON请求返回异常
【发布时间】:2013-05-17 05:48:41
【问题描述】:

-我通过将JSON 请求从数组转换到远程Windows Server 2008 R2 来发送JSON 网络服务。

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

【问题讨论】:

  • 可能是这个问题:stackoverflow.com/questions/2067742/…help
  • 这是服务器端问题。出于某种原因,它正试图将字典转换为数组。
  • @borrrden - 感谢您的回复。服务器正在使用以下代码: [WebMethod] public string DeerializeDataTable(string n) { var table = JsonConvert.DeserializeObject(n);返回“确定”; }
  • 你需要说一下DataTable是什么。此外,您需要在服务器端进行调试,而不是在客户端进行调试。这个问题需要有不同的标记。客户端很好。
  • 这个link 会很有帮助

标签: iphone .net ios objective-c json


【解决方案1】:

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

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

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

【讨论】:

  • 您能详细说明原因吗?看起来像有效的 JSON - 这是 JsonConvert .NET API 的限制吗?
  • 是的,这是服务器端的问题。
  • @Wain - 感谢重播。我试过你的代码,但它给出了一个错误 - ExceptionType = "System.InvalidOperationException"; Message = "Type 'System.String' 不支持数组的反序列化。";
  • 我不知道.net,您是否更改了服务器代码以期望字典?
猜你喜欢
  • 2015-12-02
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 2017-03-12
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 2017-03-29
相关资源
最近更新 更多