2016-08-19 183 views
0

我目前有問題尋找一個特定的值,這是一個JSON文件中的new_user,我使用file_get_contents然後解碼它。無法解析PHP中的JSON以查找特定值?

這裏是我的代碼:

<html> 
<head> 
    <link href="styles.css" rel="stylesheet" type="text/css" /> 
<title>PHP Bing</title> 
</head> 
<body><form method="post" action="<?php echo $PHP_SELF;?>"> 
Type in a search:<input type="text" id="searchText" name="searchText" value=""/> 
     <input type="submit" value="Search!" name="submit" id="searchButton" /> 
<?php 
if (isset($_POST['submit'])) { 
$request = 'https://devblogs.instavoice.com/vb?data={"cmd":"join_user","client_os_ver":"1511","client_app_ver":"vb.00.00.001","phone_num":"xxxxxxxxxxxxx","device_id":"xxxxxxxxxxxxx","phone_num_edited":"false","opr_info_edited":"false","sim_opr_mcc_mnc":"na","sim_country_iso":"in","api_ver":"2","app_secure_key":"xxxxxxxxxxx","client_os":"w","sim_serial_num":"xxxxxxxxxxxxxx"}'; 
$response = file_get_contents($request); 
$jsonobj = json_decode($response); 
foreach($json as $item) 
{ 
    if($item->new_user == "false") 
    { 
     echo "ssdasdSA"; 
    } 
else { 
echo "Sdasds"; 
} 
} 
} ?> 
</form> 
</body> 
</html> 

這裏是我試圖尋找new_user的JSON輸出:

{ 
    "cmd": "join_user", 
    "status": "ok", 
    "login_id": "918220524411", 
    "user_secure_key": "b2ff39178db49257733ff76c093329c484589459ccc617167a2a98d40dc5359a3ff89b54ab04889c", 
    "invite_sms_text": "Hi, I\u0027m using InstaVoice for Voicemail, Missed Calls, and Chatting. Connect with me on", 
    "vsms_limits": "{\"limit\":50,\"perusr_limit\":50,\"debit\":1,\"credit\":1,\"users\":[]}", 
    "docs_url": "http://devblogs.instavoice.com/vobolo/iv/docs/", 
    "mqtt_hostname": "devmqtt.instavoice.com", 
    "mqtt_port_ssl": 8883, 
    "mqtt_password": "guest", 
    "mqtt_user": "guest", 
    "iv_support_contact_ids": "[{\"support_catg_id\":\"IVSupport\",\"support_catg\":\"Help\",\"show_as_iv_user\":true,\"iv_user_id\":\"2624836\",\"phone\":\"912222222222\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/2222\\/2624836_help_a.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/2222\\/2624836_help_a.png\",\"support_send_iv\":true,\"support_send_sms\":false,\"support_send_email\":false},{\"feedback_catg_id\":\"IVFeedback\",\"feedback_catg\":\"Suggestions\",\"show_as_iv_user\":false,\"iv_user_id\":\"2624835\",\"phone\":\"911111111111\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/1111\\/2624835_suggestions.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/1111\\/2624835_suggestions.png\",\"feedback_send_iv\":true,\"feedback_send_sms\":false,\"feedback_send_email\":false}]", 
    "iv_user_id": 17198589, 
    "iv_user_device_id": 61712, 
    "screen_name": "68318465", 
    "is_profile_pic_set": false, 
    "profile_pic_uri": "http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic.jpg", 
    "thumbnail_profile_pic_uri": "http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic_thumbnail.jpg", 
    "facebook_connection": false, 
    "twitter_connection": false, 
    "fb_connected": false, 
    "tw_connected": false, 
    "fb_post_enabled": true, 
    "tw_post_enabled": true, 
    "fb_connect_url": "http://devblogs.instavoice.com/iv/fbc/", 
    "tw_connect_url": "http://devblogs.instavoice.com/iv/twc/", 
    "vsms_allowed": true, 
    "country_isd": "91", 
    "phone_len": 10, 
    "last_fetched_msg_id": 0, 
    "last_fetched_contact_trno": 0, 
    "last_fetched_msg_activity_id": 0, 
    "last_fetched_profile_trno": 0, 
    "send_email_for_iv": true, 
    "send_sms_for_iv": true, 
    "send_email_for_vb": true, 
    "send_sms_for_vb": true, 
    "send_email_for_vsms": true, 
    "send_sms_for_vsms": true, 
    "action": "", 
    "new_user": true, 
    "phone_num_edited": false, 
    "user_verified": true, 
    "obd_timegap_sec": 45, 
    "ring_expiry_min": 30, 
    "chat_hostname": "devmqtt.instavoice.com", 
    "chat_port_ssl": "8883", 
    "chat_user": "guest", 
    "chat_password": "guest" 
} 
+0

你不變量'$ json',這是'$ jsonobj'。 – Barmar

回答

1

不要比較字符串「假」,但爲布爾值false

$jsonobj = json_decode($response); 
if($jsonobj->new_user === true) echo 'new user'; 
else echo 'not new user'; 

Live demo

+0

謝謝!但爲什麼當我使用$ user_secure = $ jsonobj-> user_secure_key時出現錯誤;錯誤是注意:未定義的屬性:stdClass :: $ user_secure_key –

+0

'$ jsonobj-> user_secure_key'沒問題,並且可以工作(請參閱https://3v4l.org/Gcc6m)。如果在屬性名稱前面使用'$',就會看到錯誤,比如'$ jsonobj - > $ user_secure_key',因爲'$ user_secure_key'不是任何屬性的名稱(它是一個未定義的變量) – BeetleJuice

0

JSON不是一個數組,你不需要一個foreach循環。此外,new_user是一個布爾值,而不是一個字符串,所以你不應該將它與一個字符串進行比較。

if ($jsonobj->new_user) { 
    echo "Sdasds"; 
} else { 
    echo "ssdasdSA"; 
} 
0

鑑於原始數據,我得到的只是一個語法錯誤,提示數據中存在問題。

$j='{"cmd":"join_user","status":"ok","login_id":"918220524411","user_secure_key":"b2ff39178db49257733ff76c093329c484589459ccc617167a2a98d40dc5359a3ff89b54ab04889c","invite_sms_text":"Hi, I\u0027m using InstaVoice for Voicemail, Missed Calls, and Chatting. Connect with me on","vsms_limits":"{\"limit\":50,\"perusr_limit\":50,\"debit\":1,\"credit\":1,\"users\":[]}","docs_url":"http://devblogs.instavoice.com/vobolo/iv/docs/","mqtt_hostname":"devmqtt.instavoice.com","mqtt_port_ssl":8883,"mqtt_password":"guest","mqtt_user":"guest","iv_support_contact_ids":"[{\"support_catg_id\":\"IVSupport\",\"support_catg\":\"Help\",\"show_as_iv_user\":true,\"iv_user_id\":\"2624836\",\"phone\":\"912222222222\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/2222\\/2624836_help_a.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/2222\\/2624836_help_a.png\",\"support_send_iv\":true,\"support_send_sms\":false,\"support_send_email\":false},{\"feedback_catg_id\":\"IVFeedback\",\"feedback_catg\":\"Suggestions\",\"show_as_iv_user\":false,\"iv_user_id\":\"2624835\",\"phone\":\"911111111111\",\"profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/profile-images\\/1111\\/2624835_suggestions.png\",\"thumbnail_profile_pic_uri\":\"http:\\/\\/devblogs.instavoice.com\\/vobolo\\/thumbnails\\/1111\\/2624835_suggestions.png\",\"feedback_send_iv\":true,\"feedback_send_sms\":false,\"feedback_send_email\":false}]","iv_user_id":17198589,"iv_user_device_id":61712,"screen_name":"68318465","is_profile_pic_set":false,"profile_pic_uri":"http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic.jpg","thumbnail_profile_pic_uri":"http://devblogs.instavoice.com/vobolo/static-contents/images/default_profile_pic_thumbnail.jpg","facebook_connection":false,"twitter_connection":false,"fb_connected":false,"tw_connected":false,"fb_post_enabled":true,"tw_post_enabled":true,"fb_connect_url":"http://devblogs.instavoice.com/iv/fbc/","tw_connect_url":"http://devblogs.instavoice.com/iv/twc/","vsms_allowed":true,"country_isd":"91","phone_len":10,"last_fetched_msg_id":0,"last_fetched_contact_trno":0,"last_fetched_msg_activity_id":0,"last_fetched_profile_trno":0,"send_email_for_iv":true,"send_sms_for_iv":true,"send_email_for_vb":true,"send_sms_for_vb":true,"send_email_for_vsms":true,"send_sms_for_vsms":true,"action":"",**"new_user":true**,"phone_num_edited":false,"user_verified":true,"obd_timegap_sec":45,"ring_expiry_min":30,"chat_hostname":"devmqtt.instavoice.com","chat_port_ssl":"8883","chat_user":"guest","chat_password":"guest"}'; 

$json=json_decode($j,true); 
if(json_last_error()!==JSON_ERROR_NONE){ 

    $errs=array(
     JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', 
     JSON_ERROR_CTRL_CHAR=> 'Unexpected control character found', 
     JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', 
     JSON_ERROR_NONE  => 'No errors' 
    ); 
    echo $errs[ json_last_error() ]; 
} else { 
    foreach($json as $key => $value){ 
     /* do stuff */ 
    } 
} 
0

看起來你代碼中只有一個簡單的錯誤。 您嘗試迭代的內容($json)未定義。

嘗試改變 foreach($json as $item)foreach($jsonobj as $item)

而且$PHP_SELF似乎是不確定的。我想你可能會尋找:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">