简体   繁体   English

如何遍历这个JSON数据?

[英]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.我正在研究 Laravel 和 Sugar REST API,以便登录、从线索模块获取数据并在表格中显示到我的自定义 HTML 页面上。 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.到目前为止,我已经能够提取 JSON 格式所需的数据,但我不明白如何遍历它以便将相关数据插入到 HTML 表中。 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. {{ \\Illuminate\\Support\\Facades\\Session::get('leadsdata')}}中的数据未解码。 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.当我应用json_decode然后运行这个相同的脚本时,它给了我htmlspecialchars() 期望参数 1 是 string, object given ,所以我不能在没有专门写print_r情况下使用该对象。

  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.你可以直接在刀片中通过“->”来使用它意味着你可以在刀片中目录使用对象$data->records并在foreach中遍历。
  • why you want to json_decode.为什么要 json_decode。

Eg.例如。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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