php - 使用 Objective C iOS App 在 TextView 中显示 JSON 值

标签 php ios objective-c json

我是 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/

相关文章:

PHP 表单通过 URL 传递值

php - 如何实现通用PHP异常处理程序

ios - 在表格 View 中看不到部分

ios - iOS 7 的核心数据、iCloud 和预构建数据

ios - 如何使用 CoreGraphics 绘制一条逐渐变化边缘的线?

javascript - 为什么对 php 的简单 ajax 调用会表现出这种奇怪的行为?

PHP 5.4.12 : Undefined index error

ios - 如何判断用户是否位于特定位置?

ios - swift 每种屏幕尺寸的不同 StoryBoard

objective-c - 用 UIImage 组合 UIActivityTypeMessage