Jump to content

nested json array


nitrag

Recommended Posts

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.