简体   繁体   English

解析从网站(CoovaChilli)生成的动态JSON并将其显示到Javascript网页中?

[英]Parsing Dynamic JSON Generated From Website (CoovaChilli) And Display It Into Javascript Webpage?

i really need your help to help me solving my problem. 我真的需要您的帮助,以帮助我解决我的问题。 I setup a captive portal using CoovaChilli, and want to show the usage feedback provided by Coova JSON Interface that can be accessed via url like this http://login.domain.com:3990/json/status from the internal network where the CoovaChilli listen on. 我使用CoovaChilli设置了强制门户,并希望显示Coova JSON接口提供的使用情况反馈,该反馈可以通过内部网络(如CoovaChilli)通过url http://login.domain.com:3990/json/status进行访问听着 If you already logged in into the captive portal and then if you access that url you will see data formated like this in your browser: 如果您已经登录到强制门户网站,然后访问该URL,您将在浏览器中看到如下格式的数据:

{
    "version": "1.0",
    "clientState": 1,
    "redir": {
        "originalURL": "http://www.gstatic.com/generate_204",
        "redirectionURL": "",
        "logoutURL": "http://10.1.0.1:3990/logoff",
        "ipAddress": "10.1.0.6",
        "macAddress": "AA-BB-CC-DD-EE-FF"
    },
    "session": {
        "sessionId": "5108c39600000003",
        "userName": "user@ri",
        "startTime": 1359529249,
        "sessionTimeout": 0,
        "idleTimeout": 900
    },
    "accounting": {
        "sessionTime": 867,
        "idleTime": 0,
        "inputOctets": 1428643,
        "outputOctets": 391752,
        "inputGigawords": 0,
        "outputGigawords": 0,
        "viewPoint": "client"
    }
} 

My question is, what should i do if want to parsing that data into a webpage using * Javascript * so i can see that data formatted more nicely (html formatted). 我的问题是,如果要使用* Javascript *将数据解析为网页 ,该怎么办这样我可以看到该数据的格式更好(html格式)。 For example i can access it via url using same domain like this http://login.domain.com/status/status.html rather than unformatted data in the previous url ? 例如,我可以使用相同的域(例如http://login.domain.com/status/status.html)通过url访问它,而不是使用以前的url中的未格式化数据?

Please note : i want use javascript to parsing because the data is different by the user who logged in into that CoovaChilli, different user have different data, only user who logged in and accessing that url can see only their own statistic, so i think the best practice is use a client side language to parsing that data. 请注意:我想使用javascript进行解析,因为登录该CoovaChilli的用户的数据不同,不同的用户具有不同的数据,只有登录并访问该URL的用户才能看到他们自己的统计信息,所以我认为最佳实践是使用客户端语言来解析该数据。

Any of your help is very appreciated. 非常感谢您的帮助。 Thank you before. 谢谢你

Try this 尝试这个

This link may help you http://www.w3schools.com/json/json_intro.asp 该链接可能会对您有所帮助http://www.w3schools.com/json/json_intro.asp

<!DOCTYPE html>
<html>
<body>
<h2>JSON Object Creation in JavaScript</h2>

<p>
Name: <span id="jname"></span><br>  
Age: <span id="jage"></span><br> 
Address: <span id="jstreet"></span><br> 
Phone: <span id="jphone"></span><br> 
</p>  

<script>
var JSONObject = {
  "name":"John Johnson",
  "street":"Oslo West 16", 
  "age":33,
  "phone":"555 1234567"};
document.getElementById("jname").innerHTML=JSONObject.name  
document.getElementById("jage").innerHTML=JSONObject.age  
document.getElementById("jstreet").innerHTML=JSONObject.street  
document.getElementById("jphone").innerHTML=JSONObject.phone  
</script>

</body>
</html>

Edit 编辑

If you want to remove var json you can use ajax or jquery eg: 如果要删除var json,则可以使用ajax或jquery,例如:

 $.getJSON("your url",function(result){
    $.each(result, function(i, field){
      $("div").append(field + " ");
    });
  });

http://www.w3schools.com/jquery/ajax_getjson.asp http://www.w3schools.com/jquery/ajax_getjson.asp

  <script src="js/jquery.min.js"></script>
  <script>
  $.getJSON("http://10.1.0.1:3990/json/status?callback=?", function(data) {
    console.log(data);
  });
  </script>

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

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