简体   繁体   English

创建JSON数据并快速解析

[英]Creating JSON data and parsing to swift

I have a problem with parsing my JSON data from my Webserver correctly. 我从我的Web服务器正确解析JSON数据时遇到问题。 I tried to parse JSON data from files I found in the Internet and it worked fine but then I tried to create my one JSON data and tried to parse it in swift. 我试图从我在Internet上找到的文件中解析JSON数据,但效果很好,但是后来我尝试创建一个JSON数据并试图快速对其进行解析。 The problem is that I can see the JSON data when I call the Adress in my Browser but when I try it in Swift it doesn't work. 问题是,当我在浏览器中调用Adress时,我可以看到JSON数据,但是当我在Swift中尝试它时却无法正常工作。 I also tried to debug to see what I get as responds and the Course Array is empty. 我还尝试调试以查看响应内容,并且“课程数组”为空。

Here is my Java code: 这是我的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;
}

"Course" data from Java: 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;
}

Here is my Swift code: 这是我的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()

"Course" data from Swift: 来自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 ?? ""

    }
}

And here is the Responds in my browser: 这是我的浏览器中的响应:

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

If you have any questions or need any additional Information please ask. 如果您有任何疑问或需要任何其他信息,请询问。 Thank you. 谢谢。

Have a try with this "Course"-model: 试试这个“课程”模型:

Care: Use decodeIfPresent if the value from your JSON-Response can be null. 注意:如果您的JSON-Response中的值可以为null,请使用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)
    }
}

Can you modify your Course Model like this : 您可以这样修改课程模型吗:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM