[swift]在字符串中的构词 JSON 响应

标签: json ios Swift
发布时间: 2017/3/5 4:32:57
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图生成 JSON 响应来演示应用程序。我已经能够为基础,在 Java Android 可是有困难将其转换为 swift 这是 Java 版本︰

JSONObject response = new JSONObject("{'status': true, 'message':'success', 'data':[" +
                "{'id':'ITM10001', 'area':'Regional', 'aboveRegion':'West', 'aboveScore':'7/10', 'belowRegion':'Lagos', 'belowScore':'4/10'}," +
                "{'id':'ITM10002', 'area':'Area', 'aboveRegion':'V/Island', 'aboveScore':'8/10', 'belowRegion':'Ogba', 'belowScore':'3/10'}, " +
                "{'id':'ITM10003', 'area':'Owned Restaurants', 'aboveRegion':'Yaba', 'aboveScore':'9/10', 'belowRegion':'Sabo', 'belowScore':'2/10'}, " +
                "{'id':'ITM10004', 'area':'Franchised Restaurants', 'aboveRegion':'Idumota', 'aboveScore':'9/10', 'belowRegion':'Itafaji', 'belowScore':'3/10'}, " +
                "{'id':'ITM10005', 'area':'Owned Restaurants', 'aboveRegion':'Mushin', 'aboveScore':'10/10', 'belowRegion':'Layi', 'belowScore':'2/10'}] }");

这是我做 swift 使用网上看到这里JSONURL代码但仍然打印出的错误。任何帮助将不胜感激

 let json = JSON.parse("{\"status\": true, \"message':\"success\", \"data\":[" +
        "{\"status\": \"Approved\", \"paidDate\": \"Paid on 15 Aug 2015\", \"statusDate\": \"30 June 2015\", \"statusPrice\": \"5000\"}," +
        "{\"status\": \"Rejected\", \"paidDate\": \"Paid on 15 Jun 2015\", \"statusDate\": \"30 May 2015\", \"statusPrice\": \"7000\"}," +
        "{\"status\": \"Approved\", \"paidDate\": \"Paid on 15 July 2015\", \"statusDate\": \"30 June 2015\", \"statusPrice\": \"9000\"}," +
        "{\"status\": \"Rejected\", \"paidDate\": \"Paid on 15 May 2015\", \"statusDate\": \"30 April 2015\", \"statusPrice\": \"8000\"} ]}")

解决方法 1:

你也不需要一个第三方库,你可以使用 NSJSONSerialization

此外,小心你的报价是双引号和转义 (有是单引号在某个地方以你为榜样在哪里,它应该已经双引号)。

let str = "{\"status\": true, \"message\":\"success\", \"data\":[" + "{\"status\": \"Approved\", \"paidDate\": \"Paid on 15 Aug 2015\", \"statusDate\": \"30 June 2015\", \"statusPrice\": \"5000\"}," + "{\"status\": \"Rejected\", \"paidDate\": \"Paid on 15 Jun 2015\", \"statusDate\": \"30 May 2015\", \"statusPrice\": \"7000\"}," + "{\"status\": \"Approved\", \"paidDate\": \"Paid on 15 July 2015\", \"statusDate\": \"30 June 2015\", \"statusPrice\": \"9000\"}," + "{\"status\": \"Rejected\", \"paidDate\": \"Paid on 15 May 2015\", \"statusDate\": \"30 April 2015\", \"statusPrice\": \"8000\"}]}"

if let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
    let jsonDict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String:AnyObject] {
    println(jsonDict)
}

结果︰

[状态︰ 1,数据: ({paidDate ="有偿对 2015 年 8 月 15 日"; 状态 = 已批准; statusDate ="2015 年 6 月 30 日"; statusPrice = 5000; },{paidDate ="有偿对 2015 年 6 月 15 日"; 状态 = 已拒绝; statusDate ="2015 年 5 月 30 日"; statusPrice = 7000; },{paidDate ="有偿对 2015 年 7 月 15 日"; 状态 = 已批准; statusDate ="2015 年 6 月 30 日"; statusPrice = 9000; },{paidDate ="有偿对 2015 年 5 月 15 日"; 状态 = 已拒绝; statusDate ="2015 年 4 月 30 日"; statusPrice = 8000; }),消息︰ 成功]

更新︰

如果你不能将特定的字符串转换,请检查错误︰

var error:NSError?
if let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
    let json = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? [String:AnyObject] {
        if error == nil {
            println(json)
        } else {
            println(error)
        }
}

更新为 Swift 2.0

do {
    if let data = str.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false),
        let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject] {
            print(json)
    }
} catch let error as NSError {
    print(error)
}
赞助商