草庐IT

NSJsonserialization

全部标签

iOS - 在 Swift 中使用 NSJSONSerialization 解析 JSON 字典

我正在使用我的RESTWeb服务来获取我想要的JSON数据,这按预期工作并打印:[{"name":"Event1","genre":"Party","subtitle":"subtitle1","startDate":"2015-10-10","location":"Anywhere"},{"name":"Event2","genre":"Party","subtitle":"subtitle2","startDate":"2015-10-10","location":"Anywhere"}]所以这似乎是一个包含2个元素的数组,它们是字典。然后我尝试使用NSJSONSerializat

ios - 验证应用收据时 NSJSONSerialization 在 Swift 中导致崩溃

我正在尝试验证我的应用收据,并已使用我在网上找到的一些代码成功完成了验证。但是,我正在模拟用户在尝试验证收据时未连接到互联网并且在这样做时遇到崩溃的事件。仅当我的设备未连接到互联网时,以下do/catch方法才会在anyObj行导致崩溃:函数validateRecipt(){var响应:NSURLResponse?变种错误:NSError?varreceipt:NSData=NSData()ifleturl=NSBundle.mainBundle().appStoreReceiptURL{ifletappReceipt=NSData(contentsOfURL:url){print("

json - NSJSONSerialization with small decimal numbers

我试图在NSJSONSerialization生成的一些JSON中包含Double值0.81。代码如下:letjsonInput=["value":0.81]letdata=tryNSJSONSerialization.dataWithJSONObject(jsonInput,options:NSJSONWritingOptions.PrettyPrinted)letjson=NSString(data:data,encoding:NSUTF8StringEncoding)!print(json)输出是:{"value":0.8100000000000001}但我想看到的是:{"val

swift - 尝试在 Swift 2 上使用 NSJSONSerialization.JSONObjectWithData

我有这个代码letpath:String="http://apple.com"letlookupURL:NSURL=NSURL(string:path)!letsession=NSURLSession.sharedSession()lettask=session.dataTaskWithURL(lookupURL,completionHandler:{(data,reponse,error)inletjsonResults:AnyObjectdo{jsonResults=tryNSJSONSerialization.JSONObjectWithData(data!,options:[]

ios - NSJSONSerialization - 无法将数据转换为字符串

我在使用NSJSONSerialization从MetOfficeDatapointAPI读取JSON时遇到问题。出现以下错误ErrorDomain=NSCocoaErrorDomainCode=3840"Theoperationcouldn’tbecompleted.(Cocoaerror3840.)"(Unabletoconvertdatatostringaroundcharacter58208.根据字符位置,我已经检查并认为这是违规行{"id":"353556","latitude":"57.1893","longitude":"-5.0929","name":"SóilChao

ios - 如何在 Swift 中的 NSJSONSerialization.dataWithJSONObject 之后获得可读的 JSON

我有一些类似的代码(我在这里进行了简化):lettext="abc"letiosVersion=UIDevice.currentDevice().systemVersionletmessage=["Text":text,"IosVersion":iosVersion]ifNSJSONSerialization.isValidJSONObject(message){leturl=NSURL(string:"http://localhost:3000/api/someapi")varrequest=NSMutableURLRequest(URL:url!)vardata=NSJSONSer

linux - 在 ubuntu/linux 上使用 NSJSONSerialization 和 swift

是否可以在ubuntu上运行swift时使用NSJSONSerialization解析JSON?既然基础可用,我假设它应该是?如果没有,是否有任何其他方法可以在linux上的swift中序列化和反序列化JSON? 最佳答案 NSJSONSerializationispartialimplemented(尚未实现序列化)根据您的需要自己动手,您会发现,这是了解Swift及其可能性的最佳投资和绝佳方式。您还可以使用周围可用的开源库之一。SwiftyJSON很流行,举个例子正如SebastianOsiński所提到的,不幸的是它也使用了N

objective-c - NSJSONSerialization.JSONObjectWithData 浮点转换/舍入错误?

我的简单RubyMotion代码:data=DataParser.parse(url)error_ptr=Pointer.new(:object)json=NSJSONSerialization.JSONObjectWithData(data,options:0,error:error_ptr)该url是一个天气API,它提供一个带有温度的JSON作为float,如{"temp_c":22.4,...}。奇怪的是,float22.4被转换为22.3999938964844。如果我检查data.to_s,温度读数为22.4,所以我假设错误在NSJSONSerialization.JSON

objective-c - 如何通过 NSJSONSerialization 在 JSON 中包含空值?

我想我知道如何使用NSJSONSerialization。我打的电话是:[NSJSONSerializationdataWithJSONObject:parametersoptions:0error:&error]其中parameters是一个NSDictionary,其中包含要在JSON请求中使用的键和值。一切都很好,直到我必须使用null作为值而不是空字符串。我根本找不到将值设置为null的方法。我需要创建的json示例是:{"target"{"firstname":,"lastname":},"error":null}服务器的设置方式是它期待一个错误作为一个字符串或一个null如

objective-c - 如何通过 NSJSONSerialization 在 JSON 中包含空值?

我想我知道如何使用NSJSONSerialization。我打的电话是:[NSJSONSerializationdataWithJSONObject:parametersoptions:0error:&error]其中parameters是一个NSDictionary,其中包含要在JSON请求中使用的键和值。一切都很好,直到我必须使用null作为值而不是空字符串。我根本找不到将值设置为null的方法。我需要创建的json示例是:{"target"{"firstname":,"lastname":},"error":null}服务器的设置方式是它期待一个错误作为一个字符串或一个null如