无法在PHP中解析JSON以找到特定值?

时间:2016-08-19 22:53:24

标签: php json

我目前在搜索特定值时出现问题,该值是JSON文件中的new_user,我使用file_get_contents然后对其进行解码。

这是我的代码:

<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"
}

4 个答案:

答案 0 :(得分:1)

不要与字符串“false”进行比较,而应与布尔值false

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

Live demo

答案 1 :(得分:0)

JSON不是数组,您不需要foreach循环。此外,new_user是布尔值,而不是字符串,因此您不应将其与字符串进行比较。

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

答案 2 :(得分: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 */
    }
}

答案 3 :(得分:0)

看起来你的代码只是一个简单的错误。 您尝试迭代的内容($json)未定义。

尝试改变 foreach($json as $item)foreach($jsonobj as $item)

同样$PHP_SELF似乎未定义。我想你可能在寻找:

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