Tag: json

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

我想我知道如何使用NSJSONSerialization。 我打的电话是: [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error] 其中参数是一个NSDictionary,其中包含在JSON请求中使用的键和值。 一切都很好,所有,直到我必须使用null作为一个值,而不是一个空string。 我找不到一种方法来将该值设置为null 。 我需要创build的json的例子是: {"target" {"firstname":<firstname>, "lastname":<lastname>}, "error":null } 服务器设置的方式是,如果没有错误,那么它要么将错误当作string,要么将为空 。 有任何想法吗? 我是否错过了一个API调用或类似的东西? 谢谢!

在Restkit iOS中parsingJSON而不用对象映射

我创build了一个使用Restkit RKClient发送login细节的login页面,并获取JSON数据(无论是用户还是错误)。 我希望能够parsingJSON数据而无需对象映射,只需要JSON即可字典。 原因是这只是一个简单的login发送用户名和密码,如果我设置对象映射,我将不得不设置login对象的详细信息,然后设置路由等…这似乎是有点啰嗦。 这是我的代码 – (IBAction)login:(id)sender { [self.activityIndicator startAnimating]; NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObject:self.username.text forKey:@"username"]; [params setObject:self.password.text forKey:@"password"]; [params setObject:@"login" forKey:@"type"]; [[RKClient sharedClient] post:@"/login" params:params delegate:self]; } – (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response { if ([request isPOST]) { if ([response isJSON]) { NSLog(@"Got a JSON response back from our POST: %@", response.bodyAsString); } } [self.activityIndicator stopAnimating]; } […]

我怎样才能检查,如果我从JSON空?

我从这里得到了JSON [{ “照片”:空}] 我使用这个代码 NSMutableArray *jPhoto = [NSMutableArray arrayWithArray:(NSArray *)[jsonDict valueForKey:@"photo"]]; 我如何检查它,如果我想使用if()? 编辑 这里是JSON数据 [{"photo": [{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928459.jpg","title":"test picture","content":"this is description for test picture.\r\n\u8aac\u660e\u6587\u306a\u306e\u306b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb"} ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1304928115.jpg","title":"nothing","content":"iMirai"} ,{"image":"http:\/\/www.yohyeh.com\/upload\/shisetsu\/13157\/photo\/1303276769.jpg","title":"iMirai","content":"Staff"}]} ] 这里是我的JSONparsing器 NSError *theError = nil; NSString *URL = [NSString stringWithFormat:@"http://www.yohyeh.com/apps/get_sub_detail.php?id=%@&menu=photo",g_id]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]]; NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; NSMutableString *string […]

如何在objective-c中创buildjson

NSData* jsonDataToSendTheServer; NSDictionary *setUser = [NSDictionary dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id", @"GET_USER_INFO",@"command", @"",@"value", nil]; NSDictionary *setUser = [NSDictionary dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id", @"GET_USER_INFO",@"command", @"",@"value", nil]; NSLog(@"%@", jsonDataToSendTheServer); 这是我的代码。 当我运行上面的代码,我得到这个打印 <7b226964 223a2275 35383738 37373334 31222c22 636f6d6d 616e6422 3a224745 545f5553 45525f49 4e464f22 2c227661 6c756522 3a22227d> 我不知道我是否可以创build一个JSON。 我怎样才能解决这个问题?

(cocoa错误3840.)“(字符0附近的值无效)AFNetworking

使用GET方法从服务器中检索文件时,我得到以下错误: Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x16e81ed0 {NSDebugDescription=Invalid value around character 0.} 我已经尝试了许多不同的东西,我相信这可能与我试图获得的文件上的JSON格式有关。 这是我一直在使用的代码: _username = @"JonDoe"; NSDictionary *parameters = @{ @"username" : _username}; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; [manager GET:@"http://………." parameters:parameters success:^(AFHTTPRequestOperation *operation, id […]

parsingJSON(date)到Swift

我在Swift中的应用程序中返回了JSON,并且有一个返回给我的date的字段。 当我提到这个数据时,代码给了我一些像“/ Date(1420420409680)/”。 我如何将其转换成NSDate? 在Swift中,我已经用Objective-Ctesting了一些例子,但没有成功。

将Core Data与JSON API同步的最有效方法是什么?

将我的核心数据模式与提供JSON的远程API同步的最佳方式是什么? 目前,我正在循环查看核心数据的JSON响应中的每个字典,以查看API ID是否存在。 这很好,但是现在要做的就是删除所有不在服务器上的本地对象。 这是我的JSON数据的一个例子: [ { "id":1234, "name":"My first object", "description":"This is a long string with lots of information" }, { "id":1235, "name":"My second object", "description":"This is a long string with lots of information" } ] 目前我能想到完成这个的唯一方法就像下面这样: NSArray *jsonData = // Convert json data to array using NSJSONSerialization NSInteger fetchedCount = _fetchedResultsController.fetchedObjects.count; if (fetchedCount […]

WebAPI无法parsing多部分/表单数据文章

我试图接受来自客户端(iOS应用程序)的文章,我的代码在阅读stream时一直失败。 说消息不完整。 我一直试图让这个工作几个小时,似乎是我的消息格式或东西是错误的。 所有我想要做的是读取一个string,但我正在与谁在做iOS部分的开发人员只知道如何发送multipart / form-data而不是content-type json。 这是确切的错误: 意外的MIME多部分stream结束。 MIME多部分邮件不完整。“ 它在这里失败: await Request.Content.ReadAsMultipartAsync(provider); 头: POST http://localhost:8603/api/login HTTP/1.1 Host: localhost:8603 Accept-Encoding: gzip,deflate Content-Type: multipart/form-data; boundary=————nx-oauth216807 Content-Length: 364 Accept-Language: en-us Accept: */* Connection: keep-alive 身体: ————–nx-oauth216807 Content-Disposition: form-data; name="token" CAAH5su8bZC1IBAC3Qk4aztKzisZCd2Muc3no4BqVUycnZAFSKuleRU7V9uZCbc8DZCedYQTIFKwJbVZCANJCs4ZCZA654PgA22Nei9KiIMLsGbZBaNQugouuLNafNqIOTs9wDvD61ZA6WSTd73AVtFp9tQ1PmFGz601apUGHSimYZCjLfGBo40EBQ5z6eSMNiFeSylym1pK4PCvI17fXCmOcRix4cs96EBl8ZA1opGKVuWizOsS0WZCMiVGvT ————–nx-oauth216807– 这是WebAPI代码: public async Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { […]

JSON数据从iOS到PHP脚本

我怎样才能访问一个PHP脚本,它通过http-post接收JSON数据? 我在iOS端执行以下操作: NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/script.php"]]; [req setHTTPMethod:@"POST"]; [req setHTTPBody:data]; [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err]; 我如何在php脚本中访问这些数据? 换句话说,当我在php脚本中调用json_decode(…)时,是什么?

使用JSON数组填充UITableView

我正在Storyboard中工作,但我认为现在是面对代码的时候了… 我的服务器上有一个PHP文件,它以JSON格式输出我的MySQL数据库表的内容: { "id":"2", "name":"The King's Arms", "city":"London", "day":"Tuesday", } 我将最终需要所有的数据,但现在我只想输出城市字段作为UITableView。 我怎么去做这个?