-1

I want to show some data from JSON string by making that string to array

I have used json_decode to convert the json string into an array. Here is my json string (dd):

"{"title":"W3Schools Online Web Tutorials","description":"w3schools.com","image":"http:\/\/www.w3schools.com\/images\/colorpicker.png","url":"https:\/\/www.w3schools.com\/"}"

When I am returning the array or dd the array, that showing me the array as I intended (dd):

array:4 [▼"title" => "W3Schools Online Web Tutorials""description" => "w3schools.com""image" => "http://www.w3schools.com/images/colorpicker.png""url" => "https://www.w3schools.com/"]

But then when I am trying to show $myarray->title it is giving me error:

Trying to get property 'title' of non-object

public function showDetail(Request $request){
    $rUrl = "http://api.linkpreview.net/?key=5c59318d927ca5c5b481c89a6c18a0a2623a61d568502&q=".$request->body;
    $json_string= file_get_contents($rUrl);
    $data= json_decode($json_string,true);
    return view('showIn')->with('data', $data);


}

Expected result: W3Schools Online Web Tutorials
Actual result: Error:Trying to get property 'title' of non-object

shyam
  • 9,134
  • 4
  • 29
  • 44
Rafaeatul
  • 43
  • 8
  • 1
    Try `$myarray["title"]` (also, please stay away from w3schools, and remove the unrelated tags; this has nothing to do with javascript, ajax or laravel) –  Feb 07 '19 at 06:05
  • this is array and you are access like an object, you should access as `$myarray['title']` – Devsi Odedra Feb 07 '19 at 06:05
  • Specify how you want to access the contents of the array. e.g $data['title'] – Tim Muia Feb 07 '19 at 06:10
  • I think you should use `serialize` and `unserialize` php function, put your array into serialize function, as when in the page you want, unserilize it. here is the link https://stackoverflow.com/questions/8641889/how-to-use-php-serialize-and-unserialize – Pascal Tovohery Feb 07 '19 at 06:44

3 Answers3

1

Your JSON string is in double quotes for one, use single quotes.

Secondly, $myarray->title is what you would do if you were working with an object. Since your working with an array, do it like $myarray['title']

This will work.

$myjson = '{"title": "W3Schools Online Web Tutorials", "description":"w3schools.com","image":"http:\/\/www.w3schools.com\/images\/colorpicker.png","url":"https:\/\/www.w3schools.com\/"}';

$myarray = json_decode($myjson, true);

echo $myarray['title'];
ThunderMax
  • 90
  • 5
0

It is an array so you should be able to access it like this $data['title'];

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
0

You should use response json instead of return view('showIn')->with('data', $data);

return response()->json($data);

but if you want to print json in your view you can simply do

$rUrl = "http://api.linkpreview.net/?key=5c59318d927ca5c5b481c89a6c18a0a2623a61d568502&q=".$request->body;
$json_string= file_get_contents($rUrl);
return view('showIn')->with('json', $json_string);

In your blade template use

{{ $json }}
Free Code
  • 273
  • 2
  • 9