JSON解析文本是In Encoded Form

时间:2021-12-03 08:23:43

I am a newbie in iOS development. I parsed a JSON Data From URL like as:

我是iOS开发的新手。我解析了一个JSON数据来自URL,如:

http://www.janvajevu.com/webservice/specific_post.php?post_id=2885

And I parsed a JSON data from it like as:

我从中解析了一个JSON数据,如:

- (void)viewDidLoad
{
[super viewDidLoad];
[self.webSpinner startAnimating];
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/specific_post.php?post_id=2885"]];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}

-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length > 0)
{
    NSError* error;
    self.webDictionary= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    self.webArray=[_webDictionary objectForKey:@"data"];
}
self.headingString=[self.webArray valueForKey:@"post_title"];
NSLog(@"Web String %@",self.headingString);
[self.webSpinner stopAnimating];
self.webSpinner.hidesWhenStopped=TRUE;
NSString *headingString=[NSString stringWithFormat:@"%@",self.headingString];
NSCharacterSet *charsToTrim = [NSCharacterSet characterSetWithCharactersInString:@"()  \n\""];
self.headLabel.text=[headingString stringByTrimmingCharactersInSet:charsToTrim];
}

Then I got a response like as means I got label text as:

然后我得到了一个响应,就像我得到标签文字一样:

"\U0aaa\U0abe\U0a97\U0ab2 \U0aae\U0abe\U0ab8\U0acd\U0aa4\U0ab0"

And when I parsed English letters from like as here in my URL "author_name" contain English letters when I parsed it then it print as same as in JSON data but other language means here my URL other data contain Gujarati letters then it is parsed in to decoded or not in Gujarati letters.

当我解析英文字母时,我的网址“author_name”包含英文字母,当我解析它时,它打印与JSON数据相同,但其他语言在这里意味着我的网址其他数据包含古吉拉特语字母然后它被解析为是否在古吉拉特语字母中解码。

Here I not encoded my URL in my code then how it come in this format? Please give me solution for it.

在这里,我没有在我的代码中编码我的URL然后它是如何以这种格式出现的?请给我解决方案。

2 个解决方案

#1


1  

You need to convert ASCII(Unicode Escaped) to Unicode(UTF-8).

您需要将ASCII(Unicode Escaped)转换为Unicode(UTF-8)。

check this : http://www.rapidmonkey.com/unicodeconverter/reverse.jsp

检查一下:http://www.rapidmonkey.com/unicodeconverter/reverse.jsp

In first text box put your \U0aaa... text and click convert you will get what you want.

在第一个文本框中放置\ U0aaa ...文本并单击转换,您将得到您想要的。

Now how you can do this in Objective-C: Try this and let me know what you get.

现在你如何在Objective-C中做到这一点:试试这个,让我知道你得到了什么。

NSData *data = [self dataUsingEncoding:[NSString defaultCStringEncoding]];

NSString *unicodeString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

self.headLabel.text = asciiString;

#2


0  

try this

NSString *aDescription  =  self.webArray[0][@"post_title_slug"];
NSString  *aTitle = [[aDescription stringByRemovingPercentEncoding] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

#1


1  

You need to convert ASCII(Unicode Escaped) to Unicode(UTF-8).

您需要将ASCII(Unicode Escaped)转换为Unicode(UTF-8)。

check this : http://www.rapidmonkey.com/unicodeconverter/reverse.jsp

检查一下:http://www.rapidmonkey.com/unicodeconverter/reverse.jsp

In first text box put your \U0aaa... text and click convert you will get what you want.

在第一个文本框中放置\ U0aaa ...文本并单击转换,您将得到您想要的。

Now how you can do this in Objective-C: Try this and let me know what you get.

现在你如何在Objective-C中做到这一点:试试这个,让我知道你得到了什么。

NSData *data = [self dataUsingEncoding:[NSString defaultCStringEncoding]];

NSString *unicodeString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];

self.headLabel.text = asciiString;

#2


0  

try this

NSString *aDescription  =  self.webArray[0][@"post_title_slug"];
NSString  *aTitle = [[aDescription stringByRemovingPercentEncoding] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];