【问题标题】:Swift Json connection failed. JSON object correct?Swift Json 连接失败。 JSON对象正确吗?
【发布时间】:2016-01-05 18:51:45
【问题描述】:

我在尝试将 Json 对象放入我的 iOS 应用程序时遇到了很多问题。我从 MysqlDB 中创建了一个 Json 文件,如下所示:

<?php
//open connection to mysql db
$connection = mysqli_connect("127.0.0.1","root","","FeedStuff") or die("Err$

//fetch table rows from mysql db
$sql = "select * from articles";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . my$

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);
?>

现在 json 对象看起来不错。 plato.guru.ksz.ch/index.php 将是对象。 这些是我加载到我的数据库中的新闻文章,它们每个都有一个 ID、标题、描述、类别、链接、媒体和来源,所有这些都只是字符串(除了 ID)

现在我尝试将此 Json 文件放入我的 iOS 对象中,以在表格单元格中显示每篇文章。 我遇到了问题,但我不太确定是 Json 文件本身还是我的 iOS 应用程序的连接。

这是 swift 文件:

import UIKit

class ViewController: UIViewController {

  override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let filePath = NSURL(string: "plato.guru.ksz.ch/index.php")
    let jsonData = NSData(contentsOfURL:filePath!)
    let json = JSON(data: (jsonData)!, options: NSJSONReadingOptions.AllowFragments, error: nil)

    print(json)
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}

我手动导入了 SwiftyJSON 文件,因为我无法让它与 pod 一起使用。 现在是我的 Json 文件不正确,或者为什么我在执行程序时收到此错误: 致命错误:在展开可选值时意外发现 nil (lldb)

感谢您的帮助,这几天一直被这个问题困扰。

【问题讨论】:

    标签: php ios json swift


    【解决方案1】:

    您需要在字符串中添加 http:// 或 https://

    let filePath = NSURL(string: "http://plato.guru.ksz.ch/index.php")
    

    let filePath = NSURL(string: "https://plato.guru.ksz.ch/index.php")
    

    【讨论】:

      【解决方案2】:

      您的Content-typetext/html。设置为application/json

      用途:

       header('Content-Type: application/json');
      

      在你的 php 文件中

      【讨论】:

      • 你能告诉我这到底是做什么的吗?我的 Json 文件在上下文中不会改变。我现在正在努力解析它,因为在我在每个对象之前查看的所有教程中,它们声明了一个名称,例如:articles{"title":"Hello"} 或者他们甚至将对象放在另一个数组中。我是否需要为每个对象命名,或者即使没有特定名称也有办法解析它?
      • 这告诉客户端他们可以将结果解析为 JSON。当我上次点击你的 URL 时,它报告说它是 HTML。客户端可以随意忽略标头并对其进行解析,但您应该预料到有些人会报告错误,因为他们认为自己没有得到 JSON。
      • 您的 JSON 返回一个对象数组,因此您可以执行let a = json as? [AnyObject],这应该会为您提供数组。然后可以let title = (a[0] as? [String:AnyObject])["title"] as? String获取第一篇文章的标题
      • 它与添加的标头一起使用,现在我可以应用我在其他教程中看到的常规 JSON 解析。非常感谢:)
      【解决方案3】:

      我建议您使用 try 将 JSON 数据分配给变量 json API连接、接收JSON的教程很多。​​

      // Parse JSON
      // By the way, is your variable JSON a dictionary?
      // Here:
      
      let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
      

      【讨论】:

        猜你喜欢
        • 2019-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多