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>

2
  • 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. Commented Sep 10, 2017 at 0:28
  • Do you want to change this client side or server side? Client side -> w3schools.com/js/js_json_parse.asp ; Server side : php.net/manual/de/function.json-decode.php ; After that use the appropriate array function to iterate over the elements Commented Sep 10, 2017 at 0:29

2 Answers 2

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(';', ' ');
Sign up to request clarification or add additional context in comments.

Comments

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>

15 Comments

Also I can't edit the page it's on. Closed code. So I have to get it from 10.0.0.20:3333 is that possible?
Sure - would you prefer server- or client-side?
client side. since i cant edit the code in the script that outputs the code. its a microhttp server inside a exe file. So hte javascript must "get" the other webside. Find that string "result" and then display it. that other code you have should work because it displays what i want, it just doesnt get the remote url.
I've added an alternative with the ajax-call included to get the content.
It's not working. This is the beginning of the raw html output: <html><body bgcolor="#808080" style="font-family: monospace;"> {"result": ["9.7 - CN", "776", "593;4780;0", "593", "0;0;0", "off", "71;40", "45590", "0;0;0;0"]}<br><br>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.