Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

JSON Output in a Specific Format.

$
0
0
Hi,

I'm trying to work out how to output some data in JSON with this specific format:

      '{"rows":
         [
            {"Firstname": "John",
             "Lastname": "Smith"
            },
            {"Firstname": "Jane",
             "Lastname": "Doe"
            }
         ]
       }'


As an example, I've gotten close to what I need with something similar to this (except I don't need the index numbers)....

PHP Code:
      $name['firstname'] = 'John';
 
      $name['lastname'] = 'Smith'     
       $names
['1'] = $name;

 
      $name['firstname'] = 'Jane';
 
      $name['lastname'] = 'Doe'     
       $names
['2'] = $name 

      $this
->output
          
->set_content_type('application/json')
 
         ->set_output(json_encode($names)); 

....but it's not quite there:

'{"1":{"firstname":"John","lastname":"Smith"},"2":{"firstname":"Jane","lastname":"Doe"}}'

As you can see I need it to start with {"rows":[ and end with ]}.

Any guidance would much appreciated.

Thanks,

Steve

CodeIgniter 3.0.0

Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>