Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Help with JSON.parse

    I'm trying to modify a Linux KDE widget that gets rates from a preset list of exchange sources using javascript. I'm re-writing it so that it gets data from a user defined exchange source and json key instead of the preset list. Please keep in mind that i'm a complete novice when it comes to javascript and am working mostly by adapting existing code.

    these are the relevant lines from the main.qml that calls the script:
    Code:
    import "../code/crypto.js" as Crypto
    ...
    var result = Crypto.getRate(plasmoid.configuration.xeUrl, plasmoid.configuration.xeKey, function(rate) {
    // display rate on widget
    }
    and the relevant lines from the script crypto.js:
    Code:
    function getRate(xeUrl, xeKey, callback) {
    
    	if(xeUrl === null) return false;
    	if(xeKey === null) return false;
    	
    	request(xeUrl, function(req) {
            console.log(xeurl);
            console.log(xekey);
    	var data = JSON.parse(req.responseText);
    	var rate = data.xeKey;
            console.log(rate);
    	callback(rate);
    	});
    
    	return true;
    }
    
    function request(xeUrl, callback) {
    	var xhr = new XMLHttpRequest();
    	xhr.onreadystatechange = (function(xhr) {
    		return function() {
    			callback(xhr);
    		}
    	})(xhr);
    	xhr.open('GET', xeUrl, true);
    	xhr.send('');
    }
    As it stands, the console.log shows the correct values for variables xeUrl and xeKey are passed but after the JSON.parse the variable 'rate' remains undefined. Some guidance would be appreciated.

  2. #2
    Regular Coder
    Join Date
    Sep 2014
    Posts
    311
    Thanks
    1
    Thanked 55 Times in 53 Posts
    Not sure but try:

    var rate = data[xeKey];

  3. Users who have thanked josephm for this post:

    spmdrd (Nov 21st, 2017)

  4. #3
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by josephm View Post
    Not sure but try:

    var rate = data[xeKey];
    Thank you!!

    This works at least when the key is a simple, single word (e.g. xeKey=last or xeKey=bid).

    How would i get it to work with complex keys
    e.g.
    xeKey = result.XETHZUSD.a[0]
    xeUrl = hxxps://api.kraken.com/0/public/Ticker?pair=ETHUSD

    I've tried a few variations but so far nothing has worked.

  5. #4
    Regular Coder
    Join Date
    Sep 2014
    Posts
    311
    Thanks
    1
    Thanked 55 Times in 53 Posts
    You can create an algorithm to separate the property names and evaluate it one at a time. But there is an easier solution, eval(). Just don't go overboard with its use

    Code:
            console.log(xeurl);
            console.log(xekey);
    	var data = JSON.parse(req.responseText);
            try {
    	    var rate = eval("data." + xeKey); 
           }
           catch (err) {
               console.log(err.message);
           }
            console.log(rate);
    As I said, don't go overboard with the use of eval(). It's a pain in the neck when an error occurs inside eval().

    Some 25 odd years ago, I got a short contract to debug a rexx application. This was the days of 4GL, 5GL. The project analyst discovered eval() in rexX. He decided to introduce a "cutting edge" technology where the procedure is also data. What he did was to save the statements in the data base with its associated data. That means that the procedure is read from the database and executed. What a 30 minute debugging of a traditional approach (Cobol) resulted in a week or more.

  6. Users who have thanked josephm for this post:

    spmdrd (Nov 21st, 2017)

  7. #5
    New to the CF scene
    Join Date
    Nov 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This worked perfectly. Thanks!

  8. #6
    New to the CF scene
    Join Date
    Jan 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Some 25 odd years ago, I got a short contract to debug a rexx application. This was the days of 4GL, 5GL. The project analyst discovered eval() in rexX. He decided to introduce a "cutting edge" technology where the procedure is also data. What he did was to save the statements in the data base with its associated data


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •