-1

I am new in iOS and I am facing the problem regarding to replace the string value in foundCharacters method of NSXMLPerser.

My code is like this

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{  
    myMutableStringassignedObj=[[NSMutableString alloc]initWithString:string];
    NSLog(@"Array String: %@",myMutableStringassignedObj);
    myMutableStringassignedObj = [[myMutableStringassignedObj stringByReplacingOccurrencesOfString:@"&"
                                         withString:@"and"] mutableCopy];//I am changing string like this but it not working
    NSData *data = [myMutableStringassignedObj dataUsingEncoding:NSUTF8StringEncoding];
    responseassigneddict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"JSON DATA = %@",responseassigneddict);
}

Due to the & It's give me null value. Is there is any way to change it. Thanks in Advance!

Muju
  • 884
  • 20
  • 54

4 Answers4

2

Replace your string modification method with this

[myMutableStringassignedObj replaceOccurrencesOfString:@"&" withString:@"and" options:NSLiteralSearch range:NSMakeRange(0, myMutableStringassignedObj.length)];

After that, place your string in an NSArray or NSDictionary.

NSArray *array = @[myMutableStringassignedObj];

The problem here is: You are not decoding jsonData from NSData object. jsonData should always start with Array or Dictionary. Here, you converted NSString into NSData and trying to decode that using NSJSONSerialization. So, you are getting an error due to that. I checked the error code.

So, to decode the NSData into a jsonObject/Foundation Object, you are supposed to place your String into an NSArray or NSDictionary and then convert it in to NSData using NSJSONSerialization

NSData *data = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:nil];
NSError *error;
id responseassigneddict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"JSON DATA = %@",responseassigneddict);

Edit:

As discussed in comments, please use an XMLParser library to solve this.

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31
0

You can do it something like,

  NSString *string = @"hello & hello";

 NSString *result = [string stringByReplacingOccurrencesOfString:@"&" withString:@"and"];

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

there is no meed of convert it to mutable string!

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • I am using Mutable String that why I need to convert it. – Muju Nov 14 '16 at 07:17
  • Can you please answer my question http://stackoverflow.com/questions/43539307/how-to-post-string-with-special-character-and-thai-language-using-xml-parsing-in – Muju Apr 22 '17 at 06:42
0
 NSMutableString *String = [[NSMutableString alloc]initWithString:@"B&c"];

    [String replaceOccurrencesOfString:@"&" withString:@"and" options:NSRegularExpressionSearch range:NSMakeRange(0, String.length)];
    NSLog(@"string after replacing %@",String);

Output:

string after replacing `Bandc`
Narendra Pandey
  • 377
  • 9
  • 28
0

It's a issue of encoding the data which is having special character. Use below line code directly instead of replacing & or any Special character.

NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"];

NSString *encodedData = [myMutableStringassignedObj stringByAddingPercentEncodingWithAllowedCharacters:set]];

NSData *data = [encodedData dataUsingEncoding:NSUTF8StringEncoding];
Rubiya Faniband
  • 310
  • 2
  • 10
  • I declare NSCharacterSet *set; in .h file and then write your code from NSString. – Muju Nov 14 '16 at 08:58
  • use this NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"]; and remove the NSCharacterSet object you declared. – Rubiya Faniband Nov 14 '16 at 08:59
  • Now it give me Null value – Muju Nov 14 '16 at 09:02
  • first convert it into data and take it in dictionary . NSData *data = [NSJSONSerialization dataWithJSONObject:[NSArray arrayWithObject:myMutableStringassignedObj] options:NSJSONWritingPrettyPrinted error:nil]; NSError *error; id responseassigneddict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; NSLog(@"JSON DATA = %@",responseassigneddict); – Rubiya Faniband Nov 14 '16 at 09:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128039/discussion-between-rubiya-faniband-and-muju). – Rubiya Faniband Nov 14 '16 at 09:30