data转json swift_swift 图片转data data转图片 - CSDN
  • 1 字典转JSON字符串 var body = [ "date": “dates”,"time": timestring,"repeat" ] let data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: []) var JSONString = S...

    1 字典转JSON字符串

            var body = [ "date": “dates”,"time": timestring,"repeat" ]
            let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
            var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
           

    2 字典转Data

     

            var body = [ "date": dates,"time": timestring,"repeat": repeatstring,"remark": remarkstring, "scene_id":cuuentScene["id"]]
            let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: []) 
             

    3 Data转JSON字符串

    var body = [ "date": “dates”,"time": “timestring”, ]
            let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
            var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
            

    4 JSON字符串转Data

    var body = [ "date": dates,"time": timestring,"repeat": repeatstring,"remark": remarkstring, "scene_id":cuuentScene["id"]]
      let  data2 :Data! = try? JSONSerialization.data(withJSONObject: body, options: [])
     var  JSONString = String(data: data2, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
     var json=JSONString!.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))

     

    展开全文
  • swift4.0 data转json

    2019-04-28 00:36:45
    func nsdataToJSON(data: NSData) -> AnyObject? { do { return try JSONSerialization.jsonObject(with: data as Data, options: .mutableContainers...
    func nsdataToJSON(data: NSData) -> AnyObject? {
        do {
            return try JSONSerialization.jsonObject(with: data as Data, options: .mutableContainers) as AnyObject
        } catch {
            print(error)
        }
        return nil
    }
    展开全文
  • // Data转JSON字符串 func DataToJSONString(data: Data) -> String? { let JSONString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)); return JSONString; }...
    // Data转JSON字符串
    func DataToJSONString(data: Data) -> String? {
        let JSONString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue));
        return JSONString;
    }
    
    // JSON字符串转Data
    func JSONStringToData(json: String) -> Data? {
        let jsonData = json.data(using: String.Encoding.utf8, allowLossyConversion: false);
        return jsonData;
    }
    
    // Data转JSON(对象)  知道JSON对象是什么类型时可以强转为字典和数组
    func DataToJSONObject(data: Data) -> Any? {
        do {
            return try JSONSerialization.jsonObject(with: data , options: .mutableContainers) as AnyObject;
        } catch {
            print(error);
        }
        return nil;
    }
    
    // JSON(对象)转Data
    func JSONObjectToData(json: Any) -> Data? {
        do {
            return try JSONSerialization.data(withJSONObject: json, options: []);
        } catch {
            print(error);
        }
        return nil;
    }
    
    // 字典|数组 转JSON字符串
    func ObjectToJSONString(object: Any) -> String? {
        do {
            let data = try JSONSerialization.data(withJSONObject: object, options: []);
            let JSONString = String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue));
            return JSONString;
        } catch {
            print(error);
        }
        return nil;
    }
    
    // 字典|数组 转Data
    func ObjectToData(object: Any) -> Data? {
        do {
            return try JSONSerialization.data(withJSONObject: object, options: []);
        } catch {
            print(error);
        }
        return nil;
    }

     

    展开全文
  • #pragma mark - JSON(对象)----->JSON字符串 // 1. 原始方法 //JSON--->data--->JSON字符串 let data = try? JSONSerialization.data(withJSONObject: json, options: []) let jsonString = String...

    #pragma mark - JSON(对象)----->JSON字符串

    // 1. 原始方法
    
    //JSON--->data--->JSON字符串
    let data = try? JSONSerialization.data(withJSONObject: json, options: [])  
    let jsonString = String(data: data, encoding: String.Encoding.utf8)

    #pragma mark - JSON字符串----->JSON(对象)

    // 1、原生方法
    
    //JSON字符串-->data-->JSON对象
    let jsonString = "{\"name\":\"zhangsan\",\"age\": 18}"
    let jsonData = jsonString.data(using: String.Encoding.utf8, allowLossyConversion: false) ?? Data()
    guard let json = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else {
         return
    }
    
    
    // 2、三方框架(SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON)
    
    
    //JSON字符串-->data-->JSON对象(测试发现直接使用JSON(jsonString).arrayValue是转化不出来的)
    let data = jsonString.data(using: String.Encoding.utf8, allowLossyConversion: false) ?? Data()
    let json = JSON(data).arrayValue

    #pragma mark - JSON(对象)----->Data

    // 1.原生方法
    
    let data = try? JSONSerialization.data(withJSONObject: json, options: []) 

    #pragma mark - Data----->JSON(对象)

    // 1、原生方法
    guard let array = try? JSONSerialization.jsonObject(with: data, options: []) as? [[String: AnyObject]] else {//转化失败就返回
         return
    }
    
    //2、三方框架(SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON)
    guard let array = JSON(data!).arrayObject as? [[String: AnyObject]] else {//转化失败就返回
        return
    }

    #pragma mark - JSON字符串----->Data

    // 1、原生方法
    // string字符串按照String.Encoding.utf8这种编码格式转化为data
    if let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false) {
        //.........
    }

    #pragma mark - Data----->JSON字符串

    // 1、原生方法
    //只要符合String.Encoding.utf8这种编码的data都能转化成功
    let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
    
    
    // 2. 三方框架(SwiftyJSON: https://github.com/SwiftyJSON/SwiftyJSON)
    
    //如果转化后的字符串不是JSON类型的字符串,则输出null(注意: 该字符串的控制台输出的格式为JSON格式输出,实际类型是字符串!!!)
    //本质是先转化为JSON对象再转化为dessription!!!
    
    let jsonString = JSON(jsonData).description

     

    展开全文
  • Swift5 字典转JSON

    2019-08-19 16:35:26
    字典转JSON func convertDictionaryToJSONString(dict:NSDictionary?)->String { let data = try? JSONSerialization.data(withJSONObject: dict!, options: JSONSerialization.WritingOptions.init(rawValue: 0...
  • //数组转JSON func arrayToJson(_ arr...
  • 直接上代码吧 1、JSONString转换为字典 /// JSONString转换为字典 /// /// - Parameter jsonString: /// - Returns: func getDictionaryFromJ
  • swift JSON和字典互

    2018-07-18 17:45:19
    开发中,很多需要JSON和字典互,只是提供方法,实用,简单明了! /// 模型转JSON /// /// - Returns: JSON字符串 fileprivate func modelToJson() -> String { var modelDic = [String : String]() ...
  • Json的解码与编码操作,这里使用swift自带的类JSONDecoder 和 JSONEncoder 1、基础处理 如果你的 JSON 数据结构和你使用的 Model 对象结构一致的话,那么解析过程将会非常简单 2、自定义键值名 默认情形下 Keys ...
  • func toolsChangeToJson(info: Any) -> String{ //首先判断能不能转换 guard JSONSerialization.isValidJSONObject(info) else { PrintLog(“json转换失败”) return “” } //如果设置options为JSON
  • swift将struct转化为json

    2016-09-30 15:44:33
    struct Car: JSONSerializable {  var manufacturer: String  var model: String  var mileage: Float    var owner: Owner  }    struct Owner: JSONSerializ
  • swift json转字符串

    2019-11-25 16:29:41
    do{ letdata =tryJSONEncoder().encode(jsonProduct) self.jsonArr=String(data: data, encoding:String.Encoding.utf8)! }catch_{ ...
  • 最常见的场景便是,客户端向服务端发起网络请求,服务端返回JSON文本,然后客户端解析这个JSON文本,再把对应数据展现到页面上。 但在编程的时候,处理JSON是一件麻烦事。在不引入任何轮子的情况下,我们通常需要...
  • 我们使用swift JSONSerialization进行解析 数据分为两种 “{}“和”[]” 一.数据格式为“{}” import UIKit let jsonstr="{\"status\":\"1\",\"data\":{\"udid\":\"5bce8b974adbc6a60858d41657f1761305f62ce9\",\...
  • 1.**************从path中获取数据************ let path=Bundle.main.path(forResource: "shengshiquxianarea.plist", ofType: nil) let proviceDataArr:NSArray = NSArray(contentsOfFile:path!...
  • 在开发过程中,某中情况下,服务器的同事会要求我们把数据写成JSON的格式发送给后台。 数组里面有嵌套字典的情况,举个例子: [{"brand_id":"28","number":"1","class_id":"2"},{"brand_id":"62","number":"1",...
  • // json 字典 func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ let jsonData:Data = jsonString.data(using: .utf8)! ...
  • 这是Dictionary转Data 的方法 源码: func jsonToData(jsonDic:Dictionary<String, Any>) -> Data? { if (!JSONSerialization.isValidJSONObject(jsonDic)) { print("is not a valid json object") ....
  • 一.输入 1.Json字符串 let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}" 2.Json对象 let jsonStr = "{\"name\":\"wall\",\"age\":\"22\",\"hobby\":\"ball\"}" let jsonData=jso
  • JSON是移动端开发常用的应用层数据交换协议。最常见的场景便是,客户端向服务端发起网络请求,服务端返回JSON文本,然后客户端解析这个JSON文本,再把对应数据展现到页面上。 但在编程的时候,处理JSON是一件麻烦事...
1 2 3 4 5 ... 20
收藏数 6,077
精华内容 2,430
关键字:

data转json swift