nsjsonserialization

Error deserializing json stream

江枫思渺然 提交于 2019-12-08 07:10:57
问题 I have an NSInputStream *inputStream receiving small JSON objects from a network connection. If I read the stream to a buffer like so: NSError *err = nil; uint8_t buffer[1024]; NSMutableData *data = [[NSMutableData alloc] init]; while ([inputStream hasBytesAvailable]) { int const len = [inputStream read:buffer maxLength:sizeof(buffer)]; if (0 < len) { [data appendBytes:buffer length:len]; } } NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err]; then I get

IOS/Objective-C: Convert NSArray of Custom Objects to JSON

♀尐吖头ヾ 提交于 2019-12-08 06:06:52
问题 Based on the accepted answer to this answer, I am trying to send an array of custom objects via JSON to a server. However, the following code to serialize the objects is crashing. I think it because NSJSONSerialization can only accept an NSDictionary, not a custom object. NSArray <Offers *> *offers = [self getOffers:self.customer]; //Returns a valid array of offers as far as I can tell. NSError *error; //Following line crashes NSData * JSONData = [NSJSONSerialization dataWithJSONObject:offers

Invalid top-level type in JSON write in Swift

泪湿孤枕 提交于 2019-12-08 04:48:11
问题 i'm trying to do a PUT using AFNetworking. I need to send an array of participants in a competition, but i'm getting the "Invalid top-level type in JSON write in Swift" error. The JSON I generate is perfect, and if I try it using REST Client everything works. Here's my code: func sendUsers(onComplete: (NSError?) -> Void) { var error: NSError? var jsonData: NSData = NSJSONSerialization.dataWithJSONObject(self.createDictionaryOfParticipations(), options: NSJSONWritingOptions.allZeros, error:

Why this code with NSJSONSerialization always crashes?

随声附和 提交于 2019-12-08 04:46:38
问题 // // main.m // ASADeepDictionary // // Created by AndrewShmig on 3/10/13. // Copyright (c) 2013 AndrewShmig. All rights reserved. // #import <Foundation/Foundation.h> #import "ASADeepDictionary.h" int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *dic = @{@"key":@"value"}; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:dic]; [archiver finishEncoding];

NSJSONSerialization gives me error “Duplicate key”

眉间皱痕 提交于 2019-12-08 02:49:44
问题 I request a JSON response from a home controlling device: NSData* responseData = [NSData dataWithContentsOfURL:url]; The JSON data in responseData is complete and valid. However, if I try NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; NSLog("Error: %@", error); it gives me the following error: Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Duplicate key for object around

NSJSONSerialization returns “<null>” string

烂漫一生 提交于 2019-12-07 05:38:37
问题 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 have a problem, values that are null in the JSON object are stored as "<null>" string values in the dictionary. Is there any way to fix this or work around it? I want to avoid traversing through the entire thing and setting them to @"" . Thanks for

NSURLSessionDownloadTaskDelegate JSON response

北慕城南 提交于 2019-12-07 02:32:11
问题 I am running a background NSURLSession session and i am trying to figure out a way to get the JSON response out of one of the NSURLDownloadTaskDelegate callbacks. I have configured my session to accept JSON responses. NSURLSessionConfiguration *backgroundSession = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.Att.Locker.BackgroundUpload"]; backgroundSession.HTTPAdditionalHeaders = @{ @"Accept":@"application/json"}; session = [NSURLSession sessionWithConfiguration

NSJSONSerialization

ε祈祈猫儿з 提交于 2019-12-07 01:44:58
问题 I have problems with some public json services with serveices formatted this way jsonFlickrFeed({ "title": "Uploads from everyone", "link": "http://www.flickr.com/photos/", "description": "", "modifi ... }) NSJSONSerialization seems to be unable to make its work NSURL *jsonUrl = [NSURL URLWithString:@"http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback"]; NSError *error = nil; NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl

Swift - define a recursive type with a protocol

我与影子孤独终老i 提交于 2019-12-06 18:57:26
I want to define a type that can be serialized to a valid JSON object So for instance if a JSON can contain the following: String Number Array Object Date Boolean I would like to define a protocol with valid types protocol JsonSerializable { } typealias JSONObject = [String : JsonSerializable] typealias JSONArray = [JsonSerializable] // foundation implements serializing numbers + strings + dates etc. to JSON extension String : JsonSerializable {} extension Int : JsonSerializable {} // problem with defining dictionary and array of JSON-able types extension Dictionary : JsonSerializable {} ...

iPhone - NSURLConnection asynchronous download using URLs in NSArray

那年仲夏 提交于 2019-12-06 15:40:23
I have seen almost all the posts about NSURL on this site, and I am still stuck. I am using Xcode 4.5. I am trying to download images and display them in a UIScrollView. I want to download asynchronously download images using URLs, that get stored in an array populated using JSON. I get the URLs from a JSON grab off of my database. That works quite well and I can see the URL's being placed into the urlArray, but making the URLConnection to get the image, seems to fail. I can't get any of the images to download, or at least they don't show up in my imageArray. Here is my code and thank you for