JSON and javascipt
#16
(2012-09-16, 02:01)Mizaki Wrote: I've never had to do it that way. Have you looked at http://www.w3schools.com/xml/xml_parser.asp etc.?

So does the JSON result return as an XML String?
Reply
#17
(2012-09-15, 23:28)Mizaki Wrote: You should get an object back. For example: In the above we should get an object called "result". As you can see from your result there is also a "result". So:
Code:
result.result.type
will give you "audio" in this case. jQuery will automatically parse it into an object from the JSON.

OK....
as much as they told me it couldn't be done with jQuery:

Code:
function xbmcjson(command){
            
    var params = '{"jsonrpc": "2.0", "method": "'+ command +'",  "id": "1"}';
    
    $.ajax({
            type: 'POST',
            url: 'http://192.168.1.9:80/jsonrpc',
            data: params,
            success: function(result, textStatus, XMLHttpRequest) { console.log(result) },
            dataType: 'json',
            contentType: 'application/json'
    });
    
}

This works and sends the correct command through.

When I send Player.GetActivePlayers as the command I still can't parse the response.

I have tried setting a variable to equal to result.result.type and then call that variable but get no response.
Reply
#18
Can you look at the result object?
Image
AWXi - Ajax web interface. Wiki
Reply
#19
(2012-09-16, 12:11)Mizaki Wrote: Can you look at the result object?

Its showing up as:
Code:
[object Object]
Reply
#20
Is that with an alert? Can you console.log? In browsers that'll show you the whole object. Otherwise it'll have to be stringifed. Something like:
Code:
function objToStr(obj, indent) {
    var out = '';
    for (var e in obj) {
        if (typeof obj[e] == 'object') {
            out += indent + e + ":\n" + objToStr(obj[e], indent+'     ') + "\n";

        } else {
            out += indent + e + ": " + obj[e] + "\n";
        }
    }
    return out;
};
Image
AWXi - Ajax web interface. Wiki
Reply
#21
(2012-09-16, 15:46)Mizaki Wrote: Is that with an alert? Can you console.log? In browsers that'll show you the whole object. Otherwise it'll have to be stringifed. Something like:
Code:
function objToStr(obj, indent) {
    var out = '';
    for (var e in obj) {
        if (typeof obj[e] == 'object') {
            out += indent + e + ":\n" + objToStr(obj[e], indent+'     ') + "\n";

        } else {
            out += indent + e + ": " + obj[e] + "\n";
        }
    }
    return out;
};

No, it's not through alert. Not sure whether I can access console.log.

I'll give the stringify a shot.

would it be as simple as swapping all the obj to result:
Code:
function objToStr(result, indent) {
    var out = '';
    for (var e in result) {
        if (typeof result[e] == 'object') {
            out += indent + e + ":\n" + objToStr(result[e], indent+'     ') + "\n";

        } else {
            out += indent + e + ": " + result[e] + "\n";
        }
    }
    return out;
};
Reply
#22
Add that function then call it with "result". Never actually used it. MKay used it. I guess try:
Code:
objToStr(result, 1);

Might have to play around or google for another object to string function.
Image
AWXi - Ajax web interface. Wiki
Reply
#23
(2012-09-16, 16:25)Mizaki Wrote: Add that function then call it with "result". Never actually used it. MKay used it. I guess try:
Code:
objToStr(result, 1);

Might have to play around or google for another object to string function.

Got it using JSON.stringify(obj)
Code:
function xbmcjson(command){
            
    var params = '{"jsonrpc": "2.0", "method": "'+ command +'",  "id": "1"}';
    
    $.ajax({
            type: 'POST',
            url: 'http://192.168.1.9:80/jsonrpc',
            data: params,
            success: function(result, textStatus, XMLHttpRequest){ JSON.stringify(result) },
            dataType: 'json',
            contentType: 'application/json'
    });
}

Now, next mission is to parse the info out of the string....
Reply
#24
Next little hiccup I hope someone can help with...

I can parse all the info in my JSON response, but am having difficulties with extracting the image with the thumbnail URL.
I have tried encoding and decoding and have used it directly with no luck.

With the old VFS system, I was able to post it into a Firefox browser tab and get the image, Is this possible with the new image:// setup?
When I do try I always get a response 'File Not Found'.

I'm pretty sure I'm doing it in the correct format: ie.

http://192.168.1.9:80/image/image://musi...20Girl.mp3

Am I suppose to be encoding the URL or decoding it?

Little bit lost with this....
Reply
#25
You need to URL encode the whole image:// path (including "image://") that you get from JSON-RPC and append that to the image handler i.e. http://192.168.1.9:80/image/
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#26
(2012-09-19, 16:02)Montellese Wrote: You need to URL encode the whole image:// path (including "image://") that you get from JSON-RPC and append that to the image handler i.e. http://192.168.1.9:80/image/

Does it work in a browser?
Reply
#27
Yes if you URL encoded the image:// path you can download/view the image in a browser.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#28
(2012-09-19, 21:30)Montellese Wrote: Yes if you URL encoded the image:// path you can download/view the image in a browser.

I've tried encoding this:
Code:
image://music@smb%3a%2f%2fDOE-HTPC%2fStorage%20(E)%2fMusic%2fOasis%20-%20Hello.mp3

And then appended it to http://192.168.1.9:80/image/
So I end up with a URL that is:
Code:
http://192.168.1.9:80/image/image%3A%2F%2Fmusic%40smb%253a%252f%252fDOE-HTPC%252fStorage%2520(E)%252fMusic%252fOasis%2520-%2520Hello.mp3

But it doesn't work in my app, or on either chrome or firefox browsers.
I've even tried encapsulating the "" around image:// but still no luck.
Reply
#29
That doesn't look encoded to me. You have to encode the already encoded thumbnail string. Your string should look like:
Code:
http://192.168.1.9:80/image/image%253A%252F%252Fmusic%2540smb%25253a%25252f%25252fDOE-HTPC%25252fStorage%252520%28E%29%25252fMusic%25252fOasis%252520-%252520Hello.mp3
Image
AWXi - Ajax web interface. Wiki
Reply
#30
It looks to me like you are encoding the filename instead of the thumbnail since it ends with .mp3 . You cannot simply take the filename and add image:// in front of it. You need to use the thumbnail or fanart value from the json response
Reply

Logout Mark Read Team Forum Stats Members Help
JSON and javascipt0