&;json字符串中的amp特殊字符问题


&amp special character issue in json string

我有一个字典数组,它被转换成这样的JSON字符串,

 NSMutableArray *returnArray = [NSMutableArray new];
    NSMutableDictionary *temp1= [NSMutableDictionary new];
    [temp setObject:@"item1" forKey:@"notes"];
    NSMutableDictionary *temp2= [NSMutableDictionary new];
    [temp2 setObject:@"item & item2 " forKey:@"notes"];
    NSString *json = [self aryToJSONString:allSync];

json转换器:

-(NSString *)aryToJSONString:(id) ary{
NSError *error; 
NSString *jsonString;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ary 
                                                   options:0 // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];
if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   }
    return jsonString;
}  

转换后的JSON:

[{"notes":"item"},{"notes":"item1 & item2"}]

然后将其POST到服务器(PHP)。服务器接收到如下不可解析的json字符串,

[{'"notes'":'" item1 '"},{'"notes'":'"item1 ","item2'"}]":"

如何处理&amp特殊字符问题?

编辑:

PHP代码:

 $json = $_REQUEST['json'];
       $json = stripslashes($json);
       $jsonArr = json_decode($json, true);
       while($item = array_shift($jsonArr))
       {
           foreach($item as $key=>$value)
           {
           }
       }

尝试以下代码:

在发布到服务器之前,将JSON字符串传递到下面的方法。

-(NSString*)replaceSpecialCharsFromString:(NSString*)str
{
    str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
    str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
    str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
    return str;
}