Im trying to create this with the php encode function:
{
"foo": [
{
"bar": "111"
}
]
}
But all i can manage with some php arrays and json encoding is this:
{
"foo": [
"{
\"bar\":184530"
}"
]
}
Obviously i don’t want the object as a string but as an object, so without quotes.
Here’s my PHP:
$stmt->execute();
$stmt->bind_result($bar);
while ($stmt->fetch()) {
$activity_array = array("bar" => $bar);
$activity_json = json_encode($activity_array);
$json_array[] = $activity_json;
}
$json = json_encode($json_array);
echo '{ "foo": ' .$json .'}';
Don’t encode bits of your data structure as JSON. Only encode the final data structure. Remove this line:
That causes you to have an array encoded as JSON stored in an array which is also encoded as JSON.
You want an array (encoded as JSON) that contains an array (not bits of JSON).