jjzjj

JSONSerialization

全部标签

使用 Swift 3 的 JSONSerialization

我花了很长时间来理解Swift3的简单JSON序列化原理。请问我能否获得一些帮助,将JSON从网站解码为数组,以便我可以作为jsonResult["team1"]访问它["a"]等?这是相关代码:lethttprequest=URLSession.shared.dataTask(with:myurl){(data,response,error)inself.label.text="RESULT"iferror!=nil{print(error)}else{ifleturlContent=data{do{letjsonResult=tryJSONSerialization.jsonObj

ios - JSONSerialization 和 EXC_BAD_ACCESS

我借助php服务从MYSQL数据库中获取一些信息。最后,我通过echojson_encode($resultArray)将其推送到我的应用程序。现在我遇到了JSONSerialization的问题,这是我的代码。If(urlData!=nil){letres=responseasNSHTTPURLResponse!;NSLog("Responsecode:%ld",res.statusCode);if(res.statusCode>=200&&res.statusCode%@",responseData);varerror:NSError?letjsonData2:NSDictiona

json - Swift3 JSONSerialization with [String :Any] rather than NSDictionary

假设你有一个json字符串someText并且你想将它解析为字典。以前我做过这个......letjsonResult:NSDictionary=tryJSONSerialization.jsonObject(with:someText.data(using:.utf8)!,options:JSONSerialization.ReadingOptions.mutableContainers)as!NSDictionary但这只是一个蹩脚的NSDictionary。看来你确实可以做到这一点......letjsonResult:[String:Any]=tryJSONSerializat

json - 使用 JSONSerialization 序列化货币时指定小数位数

NumberFormatter使得在屏幕上显示值时格式化货币非常容易:letdecimal=Decimal(25.99)letdecimalNumberFormatter=NumberFormatter()decimalNumberFormatter.numberStyle=.currencyAccountingletoutput=decimalNumberFormatter.string(for:decimal)//output="$25.99"以上代码适用于任何Decimal或Double值。小数位数始终与所使用的区域设置相匹配。将浮点货币值序列化为JSON并不是那么简单。具有以下

在 Swift 3 中使用可选项的 JSONSerialization

在Swift3中,具有可选值的数组的JSONSerialization失败。这在Swift2中有效(产生null作为nil的JSON值正如预期的那样是可选的)。编辑:它没有,但把它留在这里,因为这是我问问题时的错误信念。swift2:leta:[String!]=["a","b","c"]letb:[String]=["a","b","c"]lets:String!="a"letc=["a",s]NSJSONSerialization.isValidJSONObject(a)//trueNSJSONSerialization.isValidJSONObject(b)//trueNSJS

json - Swift 4 JSONSerialization.jsonObject

我正在使用Xcode9.2和Swift4。如何检查返回的json数据是否为空?现在它给我错误Type'Any'hasnosubscriptmembersatlineifjson[0]["data"]varjson:NSMutableArray=[]varnewsArray:NSMutableArray=[]leturl=URLFactory()vardata=try!NSData(contentsOf:url.getURL())asDatado{json=tryJSONSerialization.jsonObject(with:data,options:[])as!NSMutableA

ios - 什么情况下JSONSerialization.data(withJSONObject :) throw a catchable error?

JSONSerialization.data(withJSONObject:options:)(在Swift2中又称为dataWithJSONObject)被声明为throws。但是,传递无效对象会导致崩溃,而不是可捕获的错误:do{//CrashtryJSONSerialization.data(withJSONObject:NSObject(),options:[])}catch{//Neverreachedprint("Caughterror:",error)}那么为什么该方法被声明为“throws”呢?什么情况下会抛出异常?不知道导致错误发生的原因会导致很难知道如何处理错误,并

ios - JSONSerialization 选项有什么作用,它们如何改变 jsonResult?

我在我的项目中经常使用JSONSerialization。这是我的JSONSerialization代码示例:letjson=tryJSONSerialization.jsonObject(with:data!,options:[])as?[String:Any]注意:出于目的而缺少选项,我通常在我的项目中使用它们。我的问题是我不太确定这些options:[]有什么作用?我对选项的发现:NSJSONReadingMutableContainers:Specifiesthatarraysanddictionariesarecreatedasmutableobjects.NSJSONRea
12