0

I got this string output on a page. its HTML and I need to get it "human readable" Its stats from a server.

Could you help me getting it a bit more "clear"

The string printed is:

{"result": ["9.7 - CN", "0", "483;0;0", "483", "0;0;0", "off", "50;20", "45550", "0;0;0;0"]}

Is it possible to get it to be:

Version: 9.7 - CN Speed: 483 Temp/fan 50 20

and so on, any help is appreciated

This must be done client side

I now have this in a html file:

<!DOCTYPE html>
<html>
<body>
<script>
const regex = /{.*}/g;
var request = new XMLHttpRequest();
request.open('GET', 'http://10.0.0.20:3333', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var resp = request.responseText;
    var json = regex.exec(resp);
    obj = JSON.parse(json[0]);
    document.getElementById('content').innerHTML = 'Version: ' + obj.result[0] + ' 
Speed: ' + obj.result[2].split(';')[0] + ' Temp/fan ' + obj.result[6].replace(';', ' ');
  } else {
    console.log('We reached our target server, but it returned an error');
  }
};

request.onerror = function() {
  console.log('There was a connection error of some sort');
};

request.send();
</script>

<div id="content"></div>

</body>
</html>
  • If this comes from php, you could start by using json_decode($thestring) to make it more readable. And print_r or var_dump that. – deg Sep 10 '17 at 00:28
  • Do you want to change this client side or server side? Client side -> https://www.w3schools.com/js/js_json_parse.asp ; Server side : http://php.net/manual/de/function.json-decode.php ; After that use the appropriate array function to iterate over the elements – rndus2r Sep 10 '17 at 00:29

2 Answers2

3

Using JavaScript:

var res = {"result": ["9.7 - CN", "0", "483;0;0", "483", "0;0;0", "off", "50;20", "45550", "0;0;0;0"]} 
var text = 'Version: '+res.result[0]+' Speed: '+ res.result[3]+' Temp/fan '+ res.result[6].replace(';', ' ');
0

Would this help?

<?php
    $input = '{"result": ["9.7 - CN", "0", "483;0;0", "483", "0;0;0", "off", "50;20", "45550", "0;0;0;0"]}';
    $parsed = json_decode($input, true);
    echo "Version: ".$parsed["result"][0]." Speed: ".explode(";",$parsed["result"][3])[0]." Temp/fan ".str_replace(";"," ",$parsed["result"][6]);   
?>

Might not be the prettiest code but you can also do it client-side:

var json = '{"result": ["9.7 - CN", "0", "483;0;0", "483", "0;0;0", "off", "50;20", "45550", "0;0;0;0"]}';
obj = JSON.parse(json);
document.getElementById('content').innerHTML = 'Version: '+obj.result[0]+' Speed: '+obj.result[2].split(';')[0]+' Temp/fan '+obj.result[6].replace(';',' ');
<div id="content"></div>

Client-side version with AJAX call included:

(I'm not sure if it's http or https on the URL you provided but give it a try and see if it solves the problem)

const regex = /{.*}/g;
var request = new XMLHttpRequest();
request.open('GET', 'http://10.0.0.20:3333', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    var resp = request.responseText;
    var json = regex.exec(resp);
    obj = JSON.parse(json[0]);
    document.getElementById('content').innerHTML = 'Version: ' + obj.result[0] + ' 
Speed: ' + obj.result[2].split(';')[0] + ' Temp/fan ' + obj.result[6].replace(';', ' ');
  } else {
    console.log('We reached our target server, but it returned an error');
  }
};

request.onerror = function() {
  console.log('There was a connection error of some sort');
};

request.send();
<div id="content"></div>
espenlg
  • 84
  • 1
  • 10