Fish123
Fish123

Reputation: 25

convert from JSON to an array

I am new in javascript, can you please help me convert from JSON format to a normal array

             {"data": [
             {"name": "Bhengu","surname":"Nathi"},
             {"name": "Tsunami","surname":"Msipha"},
             { "name": "Fish","surname":"Lee"}
             ]};


             data= [
             {name: Bhengu, surname: Nathi},
             {name: Tsunami, surname: Msipha},
             { name: Fish, surname: Lee}
              ];

Upvotes: 0

Views: 460

Answers (3)

Liam
Liam

Reputation: 29750

{"data": [{ "name": "Bhengu","surname":"Nathi"}, { "name": "Tsunami","surname":"Msipha"}, { "name": "Fish","surname":"Lee"} ]};

heredata= [ { name: Bhengu, surname: Nathi}, { name: Tsunami, surname: Msipha},{ name: Fish, surname: Lee} ];

you can use Json2 script

you can then run:

var myObject = JSON.parse(myJSONtext, reviver);

which should give you the array you need as myObject

Upvotes: 1

binar
binar

Reputation: 1197

Use json_decode() with the second parameter true, which will force the returned objects to be converted to arrays.

$result = json_decode($jsonStr, true);
if (isset($result)) {
  do something here...
}

Check the manual entry for detailed use (ex. the $depth parameter).

Upvotes: 0

Poonam
Poonam

Reputation: 4631

you can use json_decode function of php

$json = '{"data": [{ "name": "Bhengu","surname":"Nathi"}, { "name": "Tsunami","surname":"Msipha"}, { "name": "Fish","surname":"Lee"} ]}';
$data = json_decode($json);
var_dump($data);

output will be :

object(stdClass)[1]
  public 'data' => 
    array
      0 => 
        object(stdClass)[2]
          public 'name' => string 'Bhengu' (length=6)
          public 'surname' => string 'Nathi' (length=5)
      1 => 
        object(stdClass)[3]
          public 'name' => string 'Tsunami' (length=7)
          public 'surname' => string 'Msipha' (length=6)
      2 => 
        object(stdClass)[4]
          public 'name' => string 'Fish' (length=4)
          public 'surname' => string 'Lee' (length=3)

Upvotes: 2

Related Questions