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
https://forums.phpfreaks.com/topic/202052-nested-json-array/
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
https://forums.phpfreaks.com/topic/202052-nested-json-array/#findComment-1059583
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
https://forums.phpfreaks.com/topic/202052-nested-json-array/#findComment-1059646
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.