Home » Php » How to generate json using php?

How to generate json using php?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’ve never used JSON before and I’m trying to utilize the following javascript:
http://jqueryselectcombo.googlecode.com/files/jquery.selectCombo1.2.6.js

It needs a JSON output in the following format:

[{oV: 'myfirstvalue', oT: 'myfirsttext'},
 {oV: 'mysecondvalue', oT: 'mysecondtext'}]

Could you guide me to an example on how to generate a JSON output as above, using PHP?

How to&Answers:

The simplest way would probably be to start with an associative array of the pairs you want:

$data = array("myfirstvalue" => "myfirsttext", "mysecondvalue" => "mysecondtext");

then use a foreach and some string concatenation:

$jsontext = "[";
foreach($data as $key => $value) {
    $jsontext .= "{oV: '".addslashes($key)."', oT: '".addslashes($value)."'},";
}
$jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma
$jsontext .= "]";

Or if you have a recent version of PHP, you can use the json encoding functions built in – just be careful what data you pass them to make it match the expected format.

Answer:

Once you have your PHP data, you can use the json_encode function ; it’s bundled with PHP since PHP 5.2

In your case you JSON string represents :

  • a list containing 2 elements
  • each one being an object, containing 2 properties / values

In PHP, this would create the structure you are representing :

$data = array(
    (object)array(
        'oV' => 'myfirstvalue',
        'oT' => 'myfirsttext',
    ),
    (object)array(
        'oV' => 'mysecondvalue',
        'oT' => 'mysecondtext',
    ),
);
var_dump($data);

The var_dump gets you :

array
  0 => 
    object(stdClass)[1]
      public 'oV' => string 'myfirstvalue' (length=12)
      public 'oT' => string 'myfirsttext' (length=11)
  1 => 
    object(stdClass)[2]
      public 'oV' => string 'mysecondvalue' (length=13)
      public 'oT' => string 'mysecondtext' (length=12)

And, encoding it to JSON :

$json = json_encode($data);
echo $json;

You get :

[{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]

BTW : Frolm what I remember, I’d say your JSON string is not valid-JSON data : there should be double-quotes arround the string, including the names of the objects properties

See http://www.json.org/ for the grammar.

Hope this helps 🙂

Answer:

This should be helpful: Generating JSON

Answer:

This is the php code to generate json format

<?php

    $catId = $_GET['catId'];
    $catId = $_POST['catId'];   

    $conn = mysqli_connect("localhost","root","","DBName");
    if(!$conn)
    {
        trigger_error('Could not Connect' .mysqli_connect_error());
    }

    $sql = "SELECT * FROM TableName";
    $result = mysqli_query($conn, $sql);

    $array = array();

    while($row=mysqli_fetch_assoc($result))
    {
        $array[] = $row;
    }

    echo'{"ProductsData":'.json_encode($array).'}'; //Here ProductsData is just a simple String u can write anything instead
    mysqli_close('$conn');
?>

Answer:

You can use the stdClass, add the properties and json_encode the object.

$object = new stdClass();
$object->first_property = 1;
$object->second_property = 2;

echo '<pre>';var_dump( json_encode($object) , $object );die;

Voilà!

string(40) "{"first_property":1,"second_property":2}"
object(stdClass)#43 (2) {
  ["first_property"]=>
  int(1)
  ["second_property"]=>
  int(2)
}