tags:

views:

516

answers:

3

Hi,

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?

Thank you!

Haluk

+2  A: 

This should be helpful: Generating JSON

ennuikiller
A: 

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 :-)

Pascal MARTIN
You don't actually need to cast arrays into objects. json_encode will automagically figure it should represent things as an object if you have string keys.
Jani Hartikainen
Hi,The output I got from the above code is actually different:array(2) { [0]=> object(stdClass)#1 (2) { ["oV"]=> string(12) "myfirstvalue" ["oT"]=> string(11) "myfirsttext" } [1]=> object(stdClass)#2 (2) { ["oV"]=> string(13) "mysecondvalue" ["oT"]=> string(12) "mysecondtext" } } [{"oV":"myfirstvalue","oT":"myfirsttext"},{"oV":"mysecondvalue","oT":"mysecondtext"}]
Haluk
I removed the var_dump from the above code and modified the main part as follows. Now it works fine:$data = array( array( oV => 'myfirstvalue', oT => 'myfirsttext', ), array( oV => 'mysecondvalue', oT => 'mysecondtext', ),);
Haluk
Oh, yes, I'm using the http://xdebug.org/ extension, so my var_dump is a bit different from the default one (better html-formated output ; ie, easier to debug) ;; about the cast to object : it's not necessary, but I like having them, to get objects on the PHP side too, and not only on the JSON/javascript side (I feel better having datatypes that are close on both sides)
Pascal MARTIN
A: 

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.

Amber
This worked like a charm thank you.Although if I understand correctly I should have used "json_encode" would have been better coding?
Haluk