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
303
votes
35 answers

Reading in a JSON File Using Swift

I'm really struggling with trying to read a JSON file into Swift so I can play around with it. I've spent the best part of 2 days re-searching and trying different methods but no luck as of yet so I have signed up to StackOverFlow to see if anyone…
Krivvenz
  • 3,911
  • 3
  • 22
  • 32
252
votes
10 answers

How to convert a JSON string to a dictionary?

I want to make one function in my swift project that converts String to Dictionary json format but I got one error: Cannot convert expression's type (@lvalue NSData,options:IntegerLitralConvertible ... This is my code: func…
trousout
  • 2,531
  • 2
  • 11
  • 5
78
votes
7 answers

Could not cast value of type 'NSTaggedPointerString' to 'NSNumber'

I have a Swift struct like this. struct Usage { var totalData: Double var remainingTotalData: Double init(jsonData: NSData) { var jsonDict = [String: AnyObject]() do { jsonDict = try…
Isuru
  • 30,617
  • 60
  • 187
  • 303
47
votes
4 answers

Swift: Convert struct to JSON?

I created a struct and want to save it as a JSON-file. struct Sentence { var sentence = "" var lang = "" } var s = Sentence() s.sentence = "Hello world" s.lang = "en" print(s) ...which results in: Sentence(sentence: "Hello world", lang:…
ixany
  • 5,433
  • 9
  • 41
  • 65
44
votes
4 answers

NSJSONSerialization from NSString

Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this?
aherlambang
  • 14,290
  • 50
  • 150
  • 253
34
votes
4 answers

How to include null value in the JSON via NSJSONSerialization?

I think I get it how to use the NSJSONSerialization over all. The call I am making is: [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error] where parameters is a NSDictionary which includes keys and values to use in the JSON…
ymotov
  • 1,449
  • 3
  • 17
  • 28
29
votes
5 answers

how to prevent NSJSONSerialization from adding extra escapes in URL

How do I prevent NSJSONSerialization from adding extra backslashes to my URL strings? NSDictionary *info = @{@"myURL":@"http://www.example.com/test"}; NSData data = [NSJSONSerialization dataWithJSONObject:info options:0 error:NULL]; NSString *string…
joels
  • 7,249
  • 11
  • 53
  • 94
21
votes
5 answers

JSON.NET serialize JObject while ignoring null properties

I have a JObject which is used as a template for calling RESTful web services. This JObject gets created via a parser and since it's used as a template telling the user what the endpoint schema looks like, I had to figure out a way to preserve all…
Arian Motamedi
  • 7,123
  • 10
  • 42
  • 82
20
votes
5 answers

Swift, NSJSONSerialization and NSError

The problem is when there is incomplete data NSJSONSerialization.JSONObjectWithData is crashing the application giving unexpectedly found nil while unwrapping an Optional value error instead of informing us using NSError variable. So we are unable…
Hope
  • 2,096
  • 3
  • 23
  • 40
18
votes
2 answers

NSJSONSerialization generating NSCFString* and NSTaggedPointerString*

Executing NSJSONSerialization on the following json sometimes gives me NSCFString* and sometimes NSTaggedPointerString* on string values. Does anyone know why this is the case and what NSJSONSerialization uses to determine which type it…
18
votes
6 answers

how to create json in objective-c

NSData* jsonDataToSendTheServer; NSDictionary *setUser = [NSDictionary dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id", @"GET_USER_INFO",@"command", …
meth
  • 1,887
  • 2
  • 18
  • 33
16
votes
2 answers

HTTP POST error Handling in Swift 2

I'm new here, and this is my first question... I try to write an App in Swift 2 that makes an HTTP POST request but I can't figure out how to use the new error handling of Swift 2. Can anyone tell me please how to implement the "do-try-catch" error…
Michael_mhr
  • 744
  • 1
  • 9
  • 27
15
votes
4 answers

How can you deserialize an escaped JSON string with NSJSONSerialization?

I have an iOS app that needs to process a response from a web service. The response is a serialized JSON string containing a serialized JSON object, looking something like this: "{ \"name\" : \"Bob\", \"age\" : 21 }" Note that this response is a…
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
14
votes
2 answers

NSJSONSerialization bug?

Been trying to debug a crash for the past 10 hours and finally, I simplified it to this code: NSError *error = nil; NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"payload" ofType:@"txt"]]; id obj =…
0xSina
  • 20,973
  • 34
  • 136
  • 253
13
votes
1 answer

How to get readable JSON after NSJSONSerialization.dataWithJSONObject in Swift

I have some code similar to this (I've simplified it here): let text = "abc" let iosVersion = UIDevice.currentDevice().systemVersion let message = ["Text" : text, "IosVersion" : iosVersion] if NSJSONSerialization.isValidJSONObject(message){ …
Neil Billingham
  • 2,235
  • 4
  • 23
  • 34
1
2 3
47 48