简体   繁体   English

如何在网页上显示JSON中的数据?

[英]How can I display the data from the JSON on my webpage?

I am writing a script that sends an ajax request. 我正在编写一个发送ajax请求的脚本。 The Cloud seems to response with the JSON, but how can I display the data from the JSON on my webpage? 云似乎响应了JSON,但是如何在我的网页上显示JSON中的数据?

Here the link for the pretty printed JSON. 这里是漂亮打印的JSON的链接。

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <button onclick="myFunctionPost()">Start</button> <script> function myFunctionPost() { jQuery.ajax( { url: 'https://iotmmss0018275632trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/app.svc/T_IOT_77877E443B666B7FED2F?$format=json', type: 'POST', crossDomain: true, dataType: 'jsonp', success: function( response ) { console.log(response); }, error : function(error) { console.log(error); } } ); } </script> </body> </html> 

By var responseObject = json.parse(response) to make a javascript object. 通过var responseObject = json.parse(response)来制作一个javascript对象。

And then do as you would with JS object? 然后像使用JS对象一样进行操作? Hard to tell exact code without knowing what do you wanna display, in what HTML. 在不知道您要显示什么HTML内容的情况下,很难说出确切的代码。

To achieve this you can use JSON.stringify() space argument. 为此,您可以使用JSON.stringify()空间参数。 You will also need to wrap the output with <pre> </pre> will preserve the line spacing. 您还需要使用<pre> </pre>包装输出,以保留行距。

 function myFunctionPost() { $.ajax( { url: 'https://jsonplaceholder.typicode.com/posts', type: 'GET', success: function(response) { $('#pp').html('<pre>' + JSON.stringify(response, undefined, 4) + '</pre>'); }, error: function(error) { console.log(error); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <button onclick="myFunctionPost()">Start</button> <p id="pp"></p> </body> </html> 

Source: Does jQuery have a JSON/javascript object to HTML pretty print function similar to PHP's var_dump? 资料来源: jQuery是否具有类似于PHP的var_dump的JSON / javascript对象到HTML漂亮的打印功能?

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

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