[英]data parse with json touch
我正在使用以下示例: http : //tempered.mobi/sites/default/files/JSonArticle.zip
但它在解析時顯示空值。
我的PHP代碼輸出:
{"news":[{"title":"DurianFM Android Application"},{"title":"Hari Raya"},{"title":"C"}]}
iPhone代碼:
(void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://...........jsontest.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn); // Look at the console and you can see what the restults are
NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;
// In "real" code you should surround this with try and catch
NSDictionary * dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
if (dict)
{
rows = [dict objectForKey:@"news"];
}
NSLog(@"Array: %@",rows);
[jsonreturn release];
}
當我檢查dict變量時,它總是顯示null值。 對我來說這是一個大問題。 NSData無法轉換為NSDictionary NSDictionary變量顯示為空值。
請幫忙 。
我已經通過將deserializeAsDictionary更改為反序列化來解決了這個問題
您必須更改標簽。
rows = [dict objectForKey:@"news"];
編輯
NSURL *url = [NSURL URLWithString:@"http://...........jsontest.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSDictionary *results = [jsonreturn JSONValue];
試試這個對我有用的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.