开发者

How to parse newLine characters in JSON string

开发者 https://www.devze.com 2023-04-12 23:45 出处:网络
I am trying to parse this string .... but due to \"message_o开发者_如何学运维f_the_day\" key , JSON is not able to recognize the string as it contains newLine characters (\\n).How would I parse this J

I am trying to parse this string .... but due to "message_o开发者_如何学运维f_the_day" key , JSON is not able to recognize the string as it contains newLine characters (\n).How would I parse this JSON string, rest of all JSON webservices works fine for me.

Json Response :

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



GDSFSFDS
SF
ADS
DSFS
F"}]}

Code :

I gave lot of try to solve out this issue.....but still having the same problem.

-(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.

 }

Error :

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


There is just a simple way which worked for me. The problem was with the posting the data from iPhone, if I use to enter some new line character, it creates the problem & more on, the same data was required to be shown on website too ,thats tried to replace the \n with
tag as , it can be easily parsed at website too.

Here is the code snippet :

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

I need to use br since the same web service data need to be used in website too.

I am not sure about the way of doing this, but yes It solved my problem at both ends i.e Website as well as in iPhone.


Try to replace "\n" with some other characters while parsing and again replace those characters with "\n" before assigning this value to some control.


Only @"\n" replaceby @"" will not work in objective-c and not @"\b", I was having same issue.

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

this will delete all carriage return and newline created by html page.

I solved issue by this. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号