rahul kapoot rahul kapoot - 3 months ago 11
JSON Question

Unable to parse Json in php to find particular value?

i am currently having problem searching for a particular value which is new_user in a json file that i get using file_get_contents and then i decode it. can anyone please help me with this problem.

Here is my code:

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


and here is the json output that i am trying to search new_user for :

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

Answer

Don't compare to the string "false", but to the boolean value false

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

Live demo