简体   繁体   English

responseString为null

[英]responseString is null

my webservice returns json string with new lines so It causes problem that responseString gives always null. 我的网络服务返回带有新行的json字符串,因此它引起问题,即responseString始终为null。

NSString *responseString=[[NSString alloc] initWithData:kampanyadata encoding:NSUTF8StringEncoding];

NSLog(@"%@",[NSString stringWithFormat:@"responsestring:%@",responseString]);

----responsestring:null ---- responsestring:null

how can I replaces new lines character in JSON String? 如何替换JSON字符串中的换行符?

NSString *tempString = [tempString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];

我认为您需要这样做:)

Are you sure kampanyadata is not null? 您确定kampanyadata不为null吗? If it's not null try to use NSASCIIStringEncoding like this: 如果不为null,请尝试使用NSASCIIStringEncoding如下所示:

NSString *responseString=[[NSString alloc] initWithData:kampanyadata encoding:NSASCIIStringEncoding];.

Offtopic 题外话

And btw the NSLog(); 然后通过NSLog(); method takes a NSString formatted already as parameter so you can use: 方法采用已经格式化的NSString作为参数,因此您可以使用:

NSLog(@"responsestring:%@",responseString);
    soapResults = [[NSString alloc]
                    initWithBytes: [webData mutableBytes]
                    length:[webData length]
                    encoding:NSUTF8StringEncoding];

try this this works fine for me 试试这个对我来说很好

You got some NSData, and you tried to convert it to an NSString. 您获得了一些NSData,然后尝试将其转换为NSString。 There's no JSON involved at this point. 此时不涉及JSON。 Any errors have nothing to do with JSON or newline characters whatsoever. 任何错误都与JSON或换行符无关。 Possibilities: 1. The NSData that you received is nil. 可能性:1.您收到的NSData为零。 2. The NSData that you received isn't in UTF-8 format. 2.您收到的NSData不是UTF-8格式。

Your NSLog statement is quite funny. 您的NSLog语句很有趣。 Look at the definition of NSLog - the first parameter is a format string. 查看NSLog的定义-第一个参数是格式字符串。 Instead of 代替

NSLog(@"%@",[NSString stringWithFormat:@"responsestring:%@",responseString]);

you should write 你应该写

NSLog (@"responseString:%@", responseString);

And you can pass the JSON document directly to NSJSONSerializer. 您可以将JSON文档直接传递给NSJSONSerializer。 No need to convert it to an NSString. 无需将其转换为NSString。 Actually, if the data is large, just a waste of valuable memory. 实际上,如果数据很大,那只会浪费宝贵的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 alamofire (responseString) 获取数据 - get Data from alamofire (responseString) 在NSDictionary保持为NSString时强制转换AFHTTPRequestOperation.responseString - Casting AFHTTPRequestOperation.responseString as NSDictionary stays as NSString URLSession函数类似于Alamofire的responseString吗? - URLSession function similar to Alamofire's responseString? 如何处理json输出中的responseString并解析它 - how to handle the responseString from json output and parse it JSON因AFJSONRequestOperation失败时如何获取responseString - how to get responseString when JSON failed with AFJSONRequestOperation AFnetworking 3 或 4 GET ResponseObject 如何拥有 responseString 和 ResponseData - AFnetworking 3 or 4 GET ResponseObject how to have responseString and ResponseData iOS - ASIHTTPRequest - 尝试通过 responseString 进行解析 - iOS - ASIHTTPRequest - Trying to parse through responseString 如何从WebView中的json responseString获取特定方法 - how to fetch particular method from a json responseString in webview 将 Alamofire 从 5.0 更新到 5.3 后,responseString 发生了变化 - responseString changed after update Alamofire from 5.0 to 5.3 如何检查[request responseString]是否等于其他字符串? - How to check if [request responseString] is equal to some other string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM