• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JSON Output in a Specific Format.

#1
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
Reply


Messages In This Thread
JSON Output in a Specific Format. - by Speed - 02-13-2017, 09:11 PM
RE: JSON Output in a Specific Format. - by Speed - 02-13-2017, 10:04 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.