iOS : How to extract value for a key in JSON object

标签 ios json web-services xcode4.5

  1. 我有一张二维码图片。我在 iOS 中扫描该图像并获得一个 url。
  2. 我通过 JSON 对象将该 URL 发布到网络服务并获得一个“ProjectID”作为返回。
  3. 如果该 projectID 为正值,我应该返回 YES,表明扫描的 URL 对网络服务有效,否则返回 NO。

[出于测试目的,我硬编码了应该返回 YES 的 URL。稍后我会得到扫描的网址]

如何提取我在日志中获取的项目ID并检查条件。

这是我写的

NSError *error;

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"test@yahoo.com" ,@"EmailID", @"http://www.yahoo.com", @"URL", @"", @"Phone", @"", @"UserID", nil];


   NSURL *url = [NSURL URLWithString:@"http:// some url"];

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

NSData *requestData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];

        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody: requestData];
        [request setTimeoutInterval:180];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];
       [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
       if(error || !data)
       {
           NSLog(@"JSON NOT posted");
       }
        else
        {
            id jsonObject = [NSJSONSerialization JSONObjectWithData:requestData options:NSJSONReadingAllowFragments error:Nil];

            if([jsonObject respondsToSelector:@selector(objectForKey:)])
            {
                NSDictionary *dictionaryForUserID = [jsonObject valueForKey:@"ProjID"];
                NSLog(@" Project Id = %@", dictionaryForUserID); 

    /* I get "Project ID = (null)"  here whereas I get one positive value in the log. My question is how to get the projectID in log? */

    /* log output ::

    2013-08-20 11:57:34.765 appname[585:5903]  Project Id = (null)

    2013-08-20 11:57:34.766 appname[585:5903] JSON data posted!

    2013-08-20 11:57:34.769 appname[585:c07] Data: 
    {"ProjID":"5","ProjName":null,"URL":null,"EmailID":null,"Phone":null,"userId":null}{"ProjID":"5","ProjName":null,"URL":null,"EmailID":null,"Phone":null,"userId":null}

    */

            }
            NSLog(@"JSON data posted!");
        }
    }];

最佳答案

好吧,问题在于您正在解析请求数据而不是响应数据。

试试这个:

id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];

关于iOS : How to extract value for a key in JSON object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18328801/

相关文章:

java - 检查 json 属性是否存在,是否为字符串,是否为正确的值

元素中带有分号的 MySQL JSON 路径

java - Spring-Webservices 的快速入门工具?

android - 如何将 JSON 数据作为参数从 Android 发送到 Jersey RESTful 服务

ios - xcode 项目转换为 iOSOpenDev

c# - Newtonsoft.json 序列化和反序列化基本/继承,其中类来自共享项目

发布数据时,IOS8 参数未附加到 URL

ios - iOS 中的 SOAP 网络服务

ios - 无法在 iOS7 中使用 OCMock 模拟 CLLocationManager 上的类方法

ios - 应用程序在 iOS 10.3 上 x 时间后收不到 VOIP >