【问题标题】:Creating JSON data and parsing to swift创建 JSON 数据并快速解析
【发布时间】:2018-11-13 15:55:23
【问题描述】:

我在正确解析来自 Web 服务器的 JSON 数据时遇到问题。我尝试从我在 Internet 上找到的文件中解析 JSON 数据,它运行良好,但随后我尝试创建我的一个 JSON 数据并尝试快速解析它。问题是当我在浏览器中调用地址时我可以看到 JSON 数据,但是当我在 Swift 中尝试它时它不起作用。我还尝试调试以查看我得到的响应,并且课程数组为空。

这是我的 Java 代码:

@GET
@Path("/course")
@Produces(MediaType.APPLICATION_JSON)
public List getCourse(){
    List courseList = new ArrayList();
    Course pCourse = new Course(0, "name", "ll", null);
    courseList.add(pCourse);

    return courseList;
}

来自 Java 的“课程”数据:

public int id;
public String name;
public String link;
public String imageUrl;

public Course() {
}

public Course(int id, String name, String link, String imageUrl) {
    this.id = id;
    this.name = name;
    this.link = link;
    this.imageUrl = imageUrl;
}

这是我的 Swift 代码:

URLSession.shared.dataTask(with: costumeUrl) { (data, response, err) in
    guard let data = data else{ return}
//            let dataString = String(data: data, encoding: .utf8)
//            print(dataString)
    do{
        let course = try JSONDecoder().decode([Course].self, from: data)
        print(course)
    }catch let jsonError{
        print(jsonError)
    }
    }.resume()

来自 Swift 的“课程”数据:

struct Course: Decodable {
    let id: Int
    let name: String
    let link: String
    let imageUrl: String

    init(json: [String: Any]){
        id = json["id"] as? Int ?? -1
        name = json["name"] as? String ?? ""
        link = json["link"] as? String ?? ""
        imageUrl = json["imageUrl"] as? String ?? ""

    }
}

这是我浏览器中的响应:

[{"id":0,"imageUrl":null,"link":"ll","name":"name"}]

如果您有任何问题或需要任何其他信息,请询问。 谢谢。

【问题讨论】:

  • 你能提供你的模型“课程”吗?
  • @Teetz 添加了“课程”模型

标签: java json swift web-services webserver


【解决方案1】:

试试这个“课程”模型:

注意:如果您的 JSON-Response 中的值可以为空,请使用 decodeIfPresent

class Course: Decodable {
    let id: Int
    let name: String
    let link: String
    let imageUrl: String?

    private enum CourseCodingKeys: String, CodingKey {
        case id = "id"
        case name = "name"
        case link = "link"
        case imageUrl = "imageUrl"
    }

    required init(from decoder: Decoder) throws {
        let courseContainer = try decoder.container(keyedBy: CourseCodingKeys.self)
        self.id = try courseContainer.decode(Int.self, forKey: .id)
        self.name = try courseContainer.decode(String.self, forKey: .name)
        self.link = try courseContainer.decode(String.self, forKey: .link)
        self.imageUrl = try courseContainer.decodeIfPresent(String.self, forKey: .imageUrl)
    }
}

【讨论】:

    【解决方案2】:

    你能像这样修改你的课程模型吗:

    struct Course: Decodable {
    
        let id: Int?
        let name: String?
        let link: String?
        let imageUrl: String?
    
        private enum CodingKeys: String, CodingKey {
            case id
            case name
            case link
            case imageUrl
        }
    }
    

    【讨论】:

    • 工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多