2011-10-15 35 views
0

我想解析此字符串....但由于“message_of_the_day”键,JSON无法识别字符串,因为它包含newLine字符(\ n)。我将如何解析这个JSON字符串,剩下的所有JSON web服务对我来说工作正常。如何解析JSON字符串中的newLine字符

JSON响应:

{"tblMessageOfTheDay":[{"customer_id":"1659","application_id":"1001","message_of_the_day":"Set your Message 



GDSFSFDS 
SF 
ADS 
DSFS 
F"}]} 

代码:

我给了很多尝试解决了这个问题.....但仍然有同样的问题。

-(void)DataCollectedSuccessfully:(NSString *)responseString 
{ 

    NSDictionary *results = [responseString JSONValue]; // Main code .. with didn;t work 
    id result = [responseString JSONValue]; // I tried for it, it that object won;t get recognized, but this trick didn't work. 
    NSLog(@"%@",result); 

    NSDictionary *results = [[responseString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] JSONValue]; // Even tried to remove the newLine characters & extra space. 

} 

错误:

-JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=5 \"Unescaped control character '0xa'\" UserInfo=0x6235db0 {NSLocalizedDescription=Unescaped control character '0xa'}", 
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: message_of_the_day\" UserInfo=0x6224a50 {NSUnderlyingError=0x6235e00 \"Unescaped control character '0xa'\", NSLocalizedDescription=Object value expected for key: message_of_the_day}", 
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Expected value while parsing array\" UserInfo=0x6224a80 {NSUnderlyingError=0x6235d20 \"Object value expected for key: message_of_the_day\", NSLocalizedDescription=Expected value while parsing array}", 
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Object value expected for key: tblMessageOfTheDay\" UserInfo=0x6224af0 {NSUnderlyingError=0x6224ab0 \"Expected value while parsing array\", NSLocalizedDescription=Object value expected for key: tblMessageOfTheDay}" 
) 
+0

在有效的JSON中,控制字符(U + 0000到U + 001F,其中包括\ n = U + 000A)**必须**被转义。你的JSON字符串不是有效的JSON,这就是为什么JSON解析器在抱怨它。如有疑问,http://jsonlint.com是验证JSON字符串的绝佳服务。 – 2011-10-15 07:30:49

+0

@Bavarious我已经检查出JSON解析器上的这个字符串...如果我删除多余的消息之间的空间,然后它显示我有效的JSON。但我需要坚持相同的格式... :( –

+0

你可能会能够找到一个更宽容的解析器,但我知道的两个解析器,SBJSON和JSONKit,将拒绝解析无效的JSON。 – 2011-10-15 21:18:33

回答

3

有一个简单的方法,为我工作。 问题是从iPhone发布数据,如果我使用输入一些新的行字符,它会创建更多的问题&,同样的数据也需要在网站上显示,那就试图用\ n替换\ n标记为
,它也可以在网站上轻松解析。

这里是代码片段:

[[messageView text] stringByReplacingOccurrencesOfString:@"\n" withString:@" br "]; 

我需要使用BR因为相同的Web服务数据需要在网站上使用过。

我不确定这样做的方式,但是它解决了我在两端的问题,即网站以及iPhone。

2

尝试在解析与其他一些字符替换“\ n”,这再次值分配给一些控制之前替换为“\ n”这些字符。

+1

盲目地用别的东西代替'\ n'会破坏JSON字符串,如果它包含字符串外的'\ n'。 – 2011-10-15 07:36:30

+0

@Nishant你能帮我解决一些代码片段吗,对我来说真的是凝灰岩时间... –

+0

@Bavarious你是对的......赢了;没有任何意义。即使我删除了/ n ...它显示了我的错误。我认为问题在于UTF格式从服务器返回的数据。 –

1

只有@“\ n”replaceby @“”在objective-c中不起作用,而不在@“\ b”中, 我遇到同样的问题。

NSString *jsonstring = [jsonStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@""]; 
jsonstring = [jsonstring stringByReplacingOccurrencesOfString:@"\t" withString:@""]; 

这将删除html页面创建的所有回车符和换行符。

我解决了这个问题。 :)