Liju
Liju

Reputation: 727

How to decode JSON values without key?

i have json

{"http://www.google.com/","http://www.facebook.com/","http://www.wordpress.com/",0}

How can i print like

 http://www.google.com/
 http://www.facebook.com/
 http://www.wordpress.com/

Upvotes: 2

Views: 3811

Answers (4)

user2864740
user2864740

Reputation: 61935

Start with real JSON, representing an array of values.

$json = '["http://www.google.com/","http://www.facebook.com/",".."]';

Then use json_decode.

$arr = json_decode($json);
print_r($arr);

Upvotes: 3

Satish Sharma
Satish Sharma

Reputation: 9635

you can use this function

function getUrls($string) {
        $regex = '/https?\:\/\/[^\" ]+/i';
        preg_match_all($regex, $string, $matches);
        //return (array_reverse($matches[0]));
        return ($matches[0]);
}

like this

$json_str = '{"http://www.google.com/","http://www.facebook.com/","http://www.wordpress.com/",0}';
$arr = getUrls($json_str);

echo "<pre>";
print_r($arr);

OUTPUT :

Array
(
    [0] => http://www.google.com/
    [1] => http://www.facebook.com/
    [2] => http://www.wordpress.com/
)

Upvotes: 3

Alexander Nenkov
Alexander Nenkov

Reputation: 2910

The lame answer :)

$json = '{"http://www.google.com/","http://www.facebook.com/","http://www.wordpress.com/",0}';
$json = str_replace(array('{', '}'), array('[',']'), $json);
print_r(json_decode($json));

Upvotes: 2

Ashouri
Ashouri

Reputation: 906

look at this sample code in php

  <?php
   $jsonData = '{ "user":"John", "age":22, "country":"United States" }';
   $phpArray = json_decode($jsonData);
   print_r($phpArray);
   foreach ($phpArray as $key => $value) { 
   echo "<p>$key | $value</p>";
   }
  ?>

Upvotes: 2

Related Questions