Search code examples
iphonejsonparsingios4ios-simulator

How to parse newLine characters in JSON string


I am trying to parse this string .... but due to "message_of_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}"
)

Solution

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