Questions tagged [nsjsonserialization]

NSJSONSerialization class is used to convert JSON to Foundation objects and Foundation objects to JSON

NSJSONSerialization class is used to convert JSON to Foundation objects and Foundation objects to JSON.

More information: NSJSONSerialization Class Reference

709 questions
8
votes
3 answers

NSJSONSerialization results in EXC_BAD_ACCESS

Currently I am writing an app (Target iOS 6, ARC enabled) that uses JSON for data transmission and Core Data for persistent storage. The JSON data is generated out of a MySQL database by a PHP script via json_encode. My Problem is that with data…
7
votes
3 answers

Invalid top-level type in JSON write

I'm trying to create a simple JSON object but I still get error and I know what's wrong in my code: NSString *vCard = [BRContacts getContacts]; // this is just a string, could be nil NSDictionary *JSONdic = nil; if (vCard) { JSONdic =…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
7
votes
5 answers

iOS-Swift : Serialize a NSManagedObject to JSON (with relationships)

After spending too much time trying to find the best practices, here I am once again asking for some help, hoping I am not the only one struggling with this : I have NSManaged Objects like these : import Foundation import CoreData class…
H4Hugo
  • 2,570
  • 3
  • 16
  • 32
7
votes
3 answers

How to serialize UIImage to JSON?

I am using imageData = UIImagePNGRepresentation(imgvw.image); and while posting [dic setObject:imagedata forKey:@"image"]; after NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted…
user2181966
  • 73
  • 1
  • 4
7
votes
2 answers

NSJSONSerialization crashing app

I have a dictionary that when I log it shows... { Date = "2013-04-30 17:17:18 +0000"; Description = Kb; EventID = "92193e58-c04a-4233-9a6c-1332bc056b20"; Title = Keyboard; } I'm trying to turn it into NSData for a JSON web service…
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
7
votes
1 answer

object returned from NSJSONSerialization can vary

Is the following statement correct, or am I missing something? You have to check the return object of NSJSONSerialization to see if it is a dictionary or an array - you can have data = {"name":"joe", "age":"young"} // NSJSONSerialization returns a…
user1705452
  • 116
  • 3
7
votes
2 answers

Unescaped control characters in NSJSONSerialization

I have this JSON http://www.progmic.com/ielts/retrive.php that I need to parse. When I do it with NSJSONSerialization, I get "Unescaped control character around character 1981" error. I need to know: What the heck are the unescaped control…
Umair Khan Jadoon
  • 2,874
  • 11
  • 42
  • 63
6
votes
3 answers

Check if response from API is valid JSON

Is there a way with NSJSONSerialization to check that the NSData is valid JSON? I don't want the application to error out if the API returns invalid JSON for some reason. NSData *data = [NSData dataWithContentsOfURL:[NSURL…
Bot
  • 11,868
  • 11
  • 75
  • 131
6
votes
1 answer

JSONSerialization.jsonObject causes memory leak in swift 4.0 and 4.2

I am developing an iOS app, using swift 4.0 (4.2 has the same issue). I added an extension to Encodable extension Encodable{ func toDict() throws -> [String:Any]? { let jsonEncoder = JSONEncoder() let jsonData = try…
Dev138
  • 334
  • 1
  • 13
6
votes
2 answers

Decoding arbitrary json field dynamically in Swift

TL;DR Is there a way that I can use JSONDecoder and write a function which will just read out from given json given field value of specified decodable type? Imaging I have the following json: { "product":{ "name":"PR1", "price":20 …
frangulyan
  • 3,580
  • 4
  • 36
  • 64
6
votes
1 answer

Swift leaks when using NSJSONSerialization

I want to write an extension for NSDictionary, so I can easily create it from the data. I wrote it like this: extension Dictionary { init?(data: NSData?) { guard let data = data else { return nil } // TODO: This leaks. if…
Damian Dudycz
  • 2,622
  • 19
  • 38
6
votes
2 answers

Use NSSerialization.datawithJSON in Swift 2

Been trying to get this to work in Swift 2.0, the error says: Type NSJSONWritingOptions cannot conform to protocol NilLiteralConvertible at var options = prettyPrinted...: func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> String…
Jackspicer
  • 1,787
  • 2
  • 13
  • 21
6
votes
2 answers

NSErrorFailingURLStringKey (kCFErrorDomainCFNetwork error -1017) for specific requests

I wrote a helper class with which I make all my requests to a server (Rails 4.1). Most of the requests within this helper class work just fine, but one specific request is failing all the time. I'm using Alamofire for my requests. Here's some of the…
Jeehut
  • 20,202
  • 8
  • 59
  • 80
6
votes
4 answers

Convert JSON to NSArray

I have a JSON array being output on a page. I would like to convert this JSON array into an NSArray. This is the JSON output on the web page: [{"city":"Current Location"},{"city":"Anaheim, CA"},{"city":"Los Angeles, CA"},{"city":"San Diego,…
Brandon
  • 1,701
  • 3
  • 16
  • 26
6
votes
2 answers

NSJSONSerialization returns "" string

I'm try to set an NSDictionary to a JSON object retrieved from the server, I'm doing that in this line: _peopleArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; It works fine and properly creates the dictionary. However, I…
Carpetfizz
  • 8,707
  • 22
  • 85
  • 146
1 2
3
47 48