我是 Objective C 的新手,但我正在尝试让我的 JSON 数据显示在我声明的 TextView 中。 ( TextView 称为 TextView )这是从 PHP 页面发送的我的 JSON 数据:
{
"userName": "derekshull",
"userBio": "This is derekshulls bio",
"userSubmitted": "15"
}
这是我的 Objective C 代码,它请求 PHP url 并获取 JSON 代码:
NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
NSArray *jsonArray = (NSArray *)myJSON;
for (id element in jsonArray) {
textview.text = [NSLog(@"Element: %@", [element description])];
}
JSON 正在调试库中加载。我已经通过 stackoverflow 和互联网搜索并搜索了 3 天,这就是我想出的,但我不明白为什么它不打印到 textview。
最佳答案
NSURL *myURL = [[NSURL alloc]initWithString:@"http://domain.com/json.php"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
if(!error)
{
for (id element in jsonArray) {
textview.text = [NSString stringWithFormat:@"Element: %@",element];
// text view will contain last element from the loop
}
}
else{
textview.text = [NSString stringWithFormat:@"Error--%@",[error description]];
}
关于php - 使用 Objective C iOS App 在 TextView 中显示 JSON 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22617778/