简体   繁体   中英

How to traverse through this JSON data?

I'm working on Laravel and Sugar REST API in order to login, fetch data from the leads module and display onto my custom HTML page in a table. So far I have been able to extract the data required in JSON format, but I do not understand how to traverse it so that I can have relevant data inserted into the HTML table. I have tried to test the print through the following code:

<!DOCTYPE html>
<html>
<head>
<title> Login Application </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<style type="text/css">
    .box {
        width: 600px;
        margin: 0 auto;
        border: 1px solid #ccc;
    }
</style>
</head>
<body>
<br/>
<nav class="navbar navbar-default">
<div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="{{url('/main/successlogin')}}">Hello User!</a>
    </div>
    <ul class="nav navbar-nav">
        <li class="active"><a href="#">Leads</a></li>
    </ul>
</div>
</nav>

@if(\Illuminate\Support\Facades\Session::has('leadsdata'))
<div>
    <pre>
       {{ \Illuminate\Support\Facades\Session::get('leadsdata')}}
    </pre>
</div>

@endif


</body>
</html>

This is the result that I get: Screenshot

There are 2 issues with this:

  1. the data in {{ \\Illuminate\\Support\\Facades\\Session::get('leadsdata')}} is not decoded. When I apply json_decode and then run this same script it gives me htmlspecialchars() expects parameter 1 to be string, object given , so I can't work with the object without having to exclusively write print_r on it.

  2. I want to only traverse the records field in the object, and pick out specific fields eg. name, assigned-to etc. for adding to the table.

How do I achieve this? I know this may seem to be a mediocre question but I can't seem to have this sorted out. Thanks in advance!

  • You can directly use it by "->" in the blade means you can directory use object $data->records and traverse in foreach in blade.
  • why you want to json_decode.

Eg.

@if(\\Illuminate\\Support\\Facades\\Session::has('leadsdata')) @foreach((\\Illuminate\\Support\\Facades\\Session::has('leadsdata'))->results as $leadsdata) {{ $leadsdata->name }} @endforeach @endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM