简体   繁体   English

如何使用SwiftyJSON解析json的特定格式?

[英]how can I parse a specific format of json with SwiftyJSON?

In my swift app I'm fetching comments from webservice. 在我的swift应用程序中,我正在从Web服务中获取评论。 The general format of incoming json is: 传入json的一般格式为:

comments =     (
            {
        "_id" = 57e460a4d9f58eb150470a0a;
        content = "fsagsd";
        "sent_at" = "2016-09-22T22:52:20.061Z";
        "username" = kamil;
    },
            {
        "_id" = 57e460c0d9f58eb150470a0b;
        content = "hfdhfd";
        "sent_at" = "2016-09-22T22:52:48.682Z";
        "username" = kamil;
    }
);

This is an actual result of: print(response.result.value) 这是以下情况的实际结果: print(response.result.value)

The whole query (with alamofire ) looks as follows: 整个查询(使用alamofire )如下所示:

Alamofire.request(.GET, "\(serverURL)/get/\(case_id)/comments/"/*, headers: headers*/)
        .validate()
        .responseJSON { response in

switch response.result {
            case .Success:
                print("success")
                if let jsonData = response.result.value as? [[String: AnyObject]] {
                    for myJSON in jsonData {
                        if let myTest = SingleComment.fromJSON(JSON(myJSON)){
                            self.items.addObject(myJSON)
                            self.myTable.reloadData()
                        }
                    }
                }

but because the comments are embedded in comments in my json - I'm never reaching the self.items.addObject(myJSON) . 但是因为注释嵌入在我的json的comments中,所以我永远不会到达self.items.addObject(myJSON) I think it would work if the incoming json looked something like: 我认为,如果传入的json看起来像这样,它将起作用:

    {
        "_id" = 57e460a4d9f58eb150470a0a;
        content = "fsagsd";
        "sent_at" = "2016-09-22T22:52:20.061Z";
        "username" = kamil;
    },
            {
        "_id" = 57e460c0d9f58eb150470a0b;
        content = "hfdhfd";
        "sent_at" = "2016-09-22T22:52:48.682Z";
        "username" = kamil;
    }

since I cannot change the incoming json - can you please help me to adjust my swift code? 由于我无法更改传入的json-您能帮助我调整我的快速代码吗?

One additional info - the fromJSON function is as follows: 一项附加信息fromJSON函数如下:

class func fromJSON(json: JSON) -> SingleComment? {
    print("single comment from json")
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
    let username = json["username"].string
    let content = json["content"].string
    let sent_at = json["sent_at"].string
    let id = json["_id"].string
    let upd = dateFormatter.dateFromString(sent_at!)

    return SingleComment(username: username!, content: content!, sent_at: upd!, id: id!)
}

Try this code in .Success block. 在.Success块中尝试此代码。 Hope it will help you. 希望对您有帮助。

    if let value = response.result.value {
                                    let data = JSON(value)
                                    if let responseDictionary = data.dictionary {
                                        if let commentsArray = responseDictionary["comments"]?.array {
                                            for commentObject in commentsArray {
                                                if let myTest = SingleComment.fromJSON(commentObject){
                                                    self.items.addObject(myJSON)

                                                }
                                            }
self.myTable.reloadData()
                                        }
                                    }
    }

Your response seems not to be a correct json data. 您的回应似乎不是正确的json数据。

Firstly, json property should be separated by : , not = . 首先,JSON属性应被分开: ,不= In your example, "_id" = 57e460c0d9f58eb150470a0b; 在您的示例中, "_id" = 57e460c0d9f58eb150470a0b; should be "_id" : 57e460c0d9f58eb150470a0b; 应该是"_id" : 57e460c0d9f58eb150470a0b; .

Secondly, the response data seems to be a jsonp format, since is contains variable and semicolon. 其次,响应数据似乎是jsonp格式,因为它包含变量和分号。 But the json format is still not correct. 但是json格式仍然不正确。 So I think you solution is not to adjust your algorithm to fit such odd "json" data, you have to ask your server, why it provide such an odd data. 因此,我认为您的解决方案不是调整算法以适合此类奇数"json"数据,而您必须询问您的服务器,为什么它会提供此类奇数数据。

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

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