How to show data from JSON string converted array

how to get data from json array in php
json string to array php
json to array
php parse json array
json_decode multidimensional array php
php json_decode example
php string to json
php json stringify

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

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'];

Convert and Loop through JSON with PHP and JavaScript Arrays , Convert JSON String to PHP Array or Object echo $someArray[0]["name"]; // Access Array data // Convert JSON string to dropdown with genders to select from, a table to display the results, and the script to handle the Ajax. The user adds the data in the User interface, the controller gets the data in Array of the object as a model which we need to supply it REST API. So we need to convert this Array of objects to JSON objects. Read Data from Database. Angular application Consumes REST API data. This data is in the format of JSON format.

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

How to convert JSON string to array of JSON objects using , How to convert JSON data to a html table using JavaScript/jQuery ? How to pretty print JSON string in JavaScript ? JavaScript | Check if a string is a valid JSON  When sending data to a web server, the data has to be a string. Convert a JavaScript object into a string with JSON.stringify() . Stringify a JavaScript Object

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 }}

JSON to PHP Using json_decode, How to use PHP's json_decode function to convert a JSON string into a PHP Typically, the JSON data will represent a JavaScript array or object literal which The following two examples demonstrate, first with an array, then with an object:. Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.

json_decode - Manual, This chapter will teach you how to exchange JSON data between the client and a PHP server. Use JSON.parse() to convert the result into a JavaScript array: var xmlhttp Send a request to the PHP file, with the JSON string as a parameter. - Jackson - Convert JSON array string to List. How to map a json which has more key/values with an array, like below? I create a different class for members and jsonmapper fails with UnrecognizedPropertyException because the DTO has a variable Member and not userId, email, etc

JSON PHP, Parse the data with JSON.parse() , and the data becomes a JavaScript object. Array as JSON Convert a string into a date, using the reviver function:. In this tutorial, you will learn, how to convert strings to arrays & split string by a comma separated array in javascript. If you want to convert array or object array to comma separated string javascript or convert object array to a comma-separated string javascript.

JSON.parse(), JSON is text format, and we can convert any JavaScript object into a JSON format​, To get an array of string values delimited by a value, see split. eval should  In this article Convert JSON to array PHP tutorial, we started with the What is JSON? How JSON works with PHP. I divided this issue into 2 sections, Convert JSON string to Array; Convert JSON string to object. JSON and PHP array both are also used for data storing. Here we start with the introduction of JSON.

Comments
  • Try $myarray["title"] (also, please stay away from w3schools, and remove the unrelated tags; this has nothing to do with javascript, ajax or laravel)
  • this is array and you are access like an object, you should access as $myarray['title']
  • Specify how you want to access the contents of the array. e.g $data['title']
  • 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 stackoverflow.com/questions/8641889/…