Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
how do i code to get the serial number when i want the following way of json format?
{
"Rec": [{
"SlNo":1
"Name": " ",
"DOB": " "
}]
}

What I have tried:


if(getenv('REQUEST_METHOD') == 'POST')
{
echo "{\"Rec\":[";
Posted
Comments
Member 11403530 9-Feb-16 9:06am    
echo json_encode(
array(
"Rec" => array(
array("SINo" => 1, "Name" => "", "DOB" => "")
)
)
);

JSON uses JavaScript syntax, learn more JSON Tutorial[^]
this
{"Rec": [{"SlNo":1,"Name": " ","DOB": " "}]}

represents a json array called "Rec" that contains one object element with these property-value pairs {"SlNo":1,"Name": " ","DOB": " "}.
One of the two ways to get the values of these property is through the object way. Learn PHP: Classes and Objects - Manual[^]
C#
<?php
$json = '{"Rec": [{"SlNo":1,"Name": " ","DOB": " "}]}';
// decode it to an object
$object = json_decode($json);
//var_dump($object);
// get its property "Rec" which is an array
$Rec = $object->Rec;
// get the first and only object element of $Rec property
$element = $Rec[0];
// get the property of "SlNo" from this object element
echo $element->SlNo;
?>

Any way is to through the associative array using
json_decode($json, true)

I will leave it to you to explore this option. Learn PHP 5 Arrays[^]
 
Share this answer
 
v2
if(getenv('REQUEST_METHOD') == 'POST')
{
echo "{\"Rec\":[";
$sql="sql statement ";
$result = mysqli_query($dbc, $sql);
$Sno=1;
while ($row = mysqli_fetch_assoc($result))
{
echo "{\"SlNo\":\"".$Sno++."\"";
echo ",\"Name\":\"".$row["name_first"]." ".$row["name_last"]."\"";
echo "}]";
echo "}";
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900