-2

How to encode JSON in below format:

{
  "jobs": [
    {
     "JobID":"1",
     "JobTitle":"CEO"
    }
  ]
}

This is what I achieved:

[
  {
    "JobID":"1",
    "JobTitle":"CEO"
  }
]

Here is my PHP script using to mysql data to JSON:

<?php
    ......

    $strSQL = "SELECT * FROM jobs WHERE 1 ";

    $objQuery = mysql_query($strSQL);
    $intNumField = mysql_num_fields($objQuery);
    $resultArray = array();
    while($obResult = mysql_fetch_array($objQuery))
    {
        $arrCol = array();
        for($i=0;$i<$intNumField;$i++)
        {
            $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
        }
        array_push($resultArray,$arrCol);
    }

    mysql_close($objConnect);

    echo json_encode($resultArray);
?>

I am not a native PHP developer, that's why not so strong in web development and i tried some tutorials and blogs but did not get any solution !

3
  • 2
    use echo json_encode("jobs"=>array($resultArray)); Commented Feb 6, 2015 at 4:58
  • (Finding duplicates is difficult for such trivial questions, because nobody bothers with even remotely descriptive question titles.) Commented Feb 6, 2015 at 5:24
  • Change your echo to: echo json_encode(array('jobs' =>$resultArray)); and you have what you're looking for exactly! Commented Aug 12, 2015 at 3:43

1 Answer 1

2

try this it should work ..

$jobarray = array();

$jobarray['jobs'] = $resultArray;

echo json_encode($jobarray);

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for solution, i already ticked your answer as well and after 7 minutes i will accept this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.