2012-12-19 26 views
2

我無法解析發送到PHP web服務的數據。我使用下面的代碼來獲得JSON:json_decode從iOS應用程序返回空值

$decoded = json_decode(file_get_contents('php://input')); 
if(is_null($decoded) == NULL) 
    { 
     $body = "Data was not successfully received"; 
     $body = $body . "  " . $jsonInput; 
    } 

的JSON是從iOS應用程序發送的,看起來像下面這樣:

{ 
    "water" : "YES", 
    "int_clean" : "YES", 
    "ceiling_stains" : "YES", 
    "additional_comments" : "not entered", 
    "roof_cond" : "YES", 
    "e_clean" : "YES", 
    "interior_cond" : "not entered", 
    "no_exp" : "not entered", 
    "addr" : "YES", 
    "roof_leak" : "YES", 
    "doors_sec" : "YES", 
    "elec" : "YES", 
    "ceiling_exp" : "not entered", 
    "repaired" : "YES", 
    "o_desc" : "not entered", 
    "w_sign" : "YES", 
    "o_cond" : "not entered", 
    "int_cond" : "YES", 
    "gas" : "YES", 
    "for_sale_sign" : "YES", 
    "sold_as_is" : "YES", 
    "mb_sign" : "YES", 
    "graffiti" : "YES", 
    "date" : "18-12-2012 18:58", 
    "dewinterized" : "YES", 
    "HVAC" : "YES", 
    "why_no_mat" : "not entered", 
    "is_lockbox" : "YES", 
    "financing_mat" : "YES", 
    "yard_cond" : "YES", 
    "marketing_mat" : "YES", 
    "HVAC_missing" : "not entered", 
    "agent_info" : "YES", 
    "e_cond" : "YES", 
    "e_key" : "YES", 
    "pool_sec" : "YES", 
    "pool_clean" : "YES" 
} 

而與此代碼發送的:

NSDictionary * info = [NSDictionary dictionaryWithObjects:values forKeys:keys]; 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info options:NSJSONWritingPrettyPrinted error:&error]; 

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://uofi-bars.com/sendEmail.php"]]; 
[req addValue:@"Content-Type: application/json" forHTTPHeaderField: @"Content-Type"]; 
[req setHTTPMethod:@"POST"]; 
[req setHTTPBody:jsonData]; 


[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

我完全不熟悉php,這已經讓我頭疼很久了。任何幫助,將不勝感激!

+0

'if(is_null($ decoded)== NULL)'?爲什麼不'if(is_null($ decode))' – Musa

+0

賓果,謝謝哈哈 – thebiglebowski11

回答

1

這是不正確if(is_null($decoded) == NULL)PHP is_null返回一個boolean值。所以你需要使用if(is_null($decoded) === FALSE)if(is_null($decoded) === TRUE)