【问题标题】:JSON Parsing text are In Encoded FormJSON解析文本是编码形式
【发布时间】:2014-12-18 06:29:00
【问题描述】:

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

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

我从中解析了一个 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];
}

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

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

当我解析来自我的 URL "author_name" 中的英文字母时,我解析它时包含英文字母,然后它的打印结果与 JSON 数据中的相同,但其他语言在这里意味着我的 URL 其他数据包含古吉拉特语字母,那么它是以古吉拉特语字母解析或不解码。

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

【问题讨论】:

  • 您希望您的标签显示古吉拉特语文本吗?
  • 是的,我想要我的标签在我的 URL 中显示相同的文本显示您显示我的 URL JSON 数据?这里我的 JSON 数据是古吉拉特语字体,当我解析它时,它就像“\U0aaa\ U0abe\U0a97\U0ab2\U0aae\U0abe\U0ab8\U0acd\U0aa4\U0ab0"
  • 您正在为哪些数据获取此类文本?对于帖子标题?还是全部?
  • @Janmenjaya 我得到了密钥“post_title”的这种类型的文本,我尝试了密钥“author_name”、“post_views”、“post_id”,然后我得到了与我的 url 中相同的数据。但我没有试试“描述”键,因为我想在 WebView 中加载这个描述键。
  • 你得到答案了吗?否则我可以修改你的功能,这将起作用。

标签: ios json parsing fonts


【解决方案1】:

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

检查这个:http://www.rapidmonkey.com/unicodeconverter/reverse.jsp

在第一个文本框中输入你的 \U0aaa... 文本并点击转换你会得到你想要的。

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

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

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

self.headLabel.text = asciiString;

【讨论】:

  • 它包含相同的值请检查它。
  • 谢谢它正在工作。
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 2021-03-15
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 2020-02-09
    • 2017-01-17
    相关资源
    最近更新 更多