簡體   English   中英

在為iPhone解析json時獲取null?

[英]Getting null while parsing json for iphone?

我試圖在這里解析JSON並執行一些操作。 我在如下所示的字符串中獲取響應,為什么json返回null,

    NSError *error = [[NSError alloc] init];
    NSHTTPURLResponse *response = nil;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSLog(@"Response code: %d", [response statusCode]);
    if ([response statusCode] >=200 && [response statusCode] <300)
    {
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@", responseData);

我在這里得到回應...

響應==> {“成功”:1} {“標簽”:“登錄”,“成功”:1,“錯誤”:0}

如果響應可以字符串形式出現,為什么它不出現在下面的代碼中? 我們能否獲取成功函數/變量或將字符串傳遞給以下代碼中的jsonData ...

 NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableLeaves error:nil];
                          NSLog(@"json data is %@",jsonData);
            NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
            NSLog(@"%d",success);

        if(success == 1)
        {
            NSLog(@"Login SUCCESS");
            [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
                   ColorPickerViewController *cpvc =[[ColorPickerViewController alloc] init];
                   [self.navigationController pushViewController:cpvc animated:YES];

        } else {

            NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
            [self alertStatus:error_msg :@"Login Failed!"];
        }

每當我運行它時,它就會執行else塊...

當我嘗試解析Json時,我將其調整為空值

2013-12-18 07:52:09.193 ColorPicker [15867:c07] json數據為(空)2013-12-18 07:52:09.193 ColorPicker [15867:c07] 0

我在這里從json發送響應

 // Get tag
   $tag = $_POST['tag'];

   // Include Database handler
   require_once 'include/DB_Functions.php';
   $db = new DB_Functions();
   // response Array
   $response = array("tag" => $tag, "success" => 0, "error" => 0);

   // check for tag type
   if ($tag == 'login') {
       // Request type is check Login
       $email = $_POST['email'];
       $password = $_POST['password'];

       // check for user
       $user = $db->getUserByEmailAndPassword($email, $password);
       if ($user != false) {
           // user found

           // echo json with success = 1
           $response["success"] = 1;
           $response["user"]["fname"] = $user["firstname"];
           $response["user"]["lname"] = $user["lastname"];
           $response["user"]["email"] = $user["email"];
   $response["user"]["uname"] = $user["username"];
           $response["user"]["uid"] = $user["unique_id"];
           $response["user"]["created_at"] = $user["created_at"];

           echo json_encode($response);
       } else {
           // user not found
           // echo json with error = 1
           $response["error"] = 1;
           $response["error_msg"] = "Incorrect email or password!";
           echo json_encode($response);
       }
   }

我使用注冊標簽幾乎可以使用相同的Web服務進行注冊,並且使用相同的代碼也可以正常使用:O

如果您的jsonData為nil。 嘗試以這種方式解析

如果是數組,您的響應應該是這樣:

Response : [{"success":1},{"tag":"login","success":1,"error":0}]

如果是字典,則響應應為

Response : {"tag":"login","success":1,"error":0}

並替換這個

    NSDictionary * jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];


    NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];

    NSInteger success = [[jsonData objectForKey:@"success"] integerValue];

要么

    NSInteger success = [[jsonData valueForKey:@"success"] integerValue];

要么

    NSInteger success = [jsonData[@"success"] integerValue];

這個回應:

響應==> {“成功”:1} {“標簽”:“登錄”,“成功”:1,“錯誤”:0}

似乎有兩個有效的json字符串:

  • {“成功”:1}並且
  • { “標記”: “登錄”, “成功”:1, “錯誤”:0}

因此,您的php代碼可能在兩個地方寫出了json。 例如,通過放置不同的成功值(兩個json字符串中的公共鍵)來調試腳本,應該有助於確定該腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM