nitrag Posted May 17, 2010 Share Posted May 17, 2010 I'm trying to output some json using PHP's json_encode function. But it seems to add extra quotations which makes it an incorrectly formatted json string...i think. PHP $rs1 = query("SELECT DISTINCT groups.group_ID, group_settings.name FROM groups, group_settings WHERE group_settings.group_ID=groups.group_ID AND groups.group_ID IN (SELECT group_ID FROM groups WHERE user_ID=1)"); $c = 0; $glist = array(); while($row = mysql_fetch_array($rs1)){ $glist[$c] = json_encode(array('id'=>$row['group_ID'],'name'=>$row['name'])); $c++; } $groups['list'] = stripslashes(json_encode($glist)); $groups['count'] = $c; $rs2 = query("SELECT COUNT(c_ID) FROM contacts WHERE group_ID='$gid'"); $row2 = mysql_fetch_assoc($rs2); $data['cCount'] = $row2['COUNT(c_ID)']; $rs4 = query("SELECT fb_ID FROM users WHERE user_ID IN (SELECT user_ID FROM groups WHERE group_ID='$gid')"); $count = 0; $list = array(); while($row4 = mysql_fetch_array($rs4)){ $list[$count] = $row4['fb_ID']; $count++; } $users = implode(',',$list); $data['users'] = $users; $data['fCount'] = $count-1; //offset for yourself $data['appstoreID'] = '331786748'; $data['groups'] = stripslashes(json_encode($groups)); $output = stripslashes(json_encode($data)); echo $output; echo "<br/>"; Output: { "cCount":"0", "users":"", "fCount":-1, "appID":"331786748", "groups": "{" list": "[" {"id":"1","name":"Test Group"}", "{"id":"2","name":"Ryan's Test Group"} "]", "count":2 }" } Quote Link to comment Share on other sites More sharing options...
GKWelding Posted May 17, 2010 Share Posted May 17, 2010 ok, what makes you think it's incorrectly formatted? Quote Link to comment Share on other sites More sharing options...
GKWelding Posted May 17, 2010 Share Posted May 17, 2010 However, your problem is that you're double encoding the $data array, more specifically get rid of $data['groups'] = stripslashes(json_encode($groups)); and replace with $data['groups'] = $groups; then change $output = stripslashes(json_encode($data)); to $output = json_encode(stripslashes($data)); Quote Link to comment Share on other sites More sharing options...
nitrag Posted May 17, 2010 Author Share Posted May 17, 2010 Thanks GKWelding, you set me back on the right track. I was getting myself all confused. Here's the correct code for others: $rs1 = query("SELECT DISTINCT groups.group_ID, group_settings.name FROM groups, group_settings WHERE group_settings.group_ID=groups.group_ID AND groups.group_ID IN (SELECT group_ID FROM groups WHERE user_ID=1)"); $c = 0; $glist = array(); while($row = mysql_fetch_array($rs1)){ $glist[$c] = array('id'=>$row['group_ID'],'name'=>$row['name']); $c++; } $groups['list'] = $glist; $groups['count'] = $c; $rs2 = query("SELECT COUNT(c_ID) FROM contacts WHERE group_ID='$gid'"); $row2 = mysql_fetch_assoc($rs2); $data['cCount'] = $row2['COUNT(c_ID)']; $rs4 = query("SELECT fb_ID FROM users WHERE user_ID IN (SELECT user_ID FROM groups WHERE group_ID='$gid')"); $count = 0; $list = array(); while($row4 = mysql_fetch_array($rs4)){ $list[$count] = $row4['fb_ID']; $count++; } $users = implode(',',$list); $data['users'] = $users; $data['fCount'] = $count-1; //offset for yourself $data['appstoreID'] = '331786748'; $data['groups'] = $groups; $output = json_encode($data); echo $output; Output: { "cCount":"0", "users":"", "fCount":-1, "appstoreID":"331786748", "groups": { "list": [ {"id":"1","name":"Test Group"}, {"id":"2","name":"Ryan's Test Group"} ], "count":2 } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.