Closed preginald closed 4 years ago
Try use that code:
ON *:TEXT:!song:#: {
var %v = now_playing_ $+ $ctime $+ $ticks
JSONOpen -df %v c:\temp\nowplaying\nowplaying.json
if ($JSONError) { msg $chan Error: There was an reading error! - Error Details: $v1 | return }
var %album = $json(%v,nowplaying,album).value
var %artist = $json(%v,nowplaying,artist).value
var %title = $json(%v,nowplaying,title).value
var %paused = $json(%v,nowplaying,paused).value
var %length = $json(%v,nowplaying,length).value
msg $chan Currently $iif(%paused,Paused,Playing) $+ : $iif(%title,$v1,N/A) - Artist: $iif(%artist,$v1,N/A) - Album: $iif(%album,$v1,N/A) - Length: $iif(%length,$duration($v1),N/A)
}
Thanks for your reply @westor7
I'm starting to understand how your code works. I'm currently getting the following error:
Error: There was an reading error! - Error Details: INVALID_JSON
I think it could be because of the way the nowplaying.json file is formatted. When I open it in vscode the entire json data appears on a single line rather than multi line. Please see attached file nowplaying.zip
OK I removed the white spaces in the json file and I'm still getting the _INVALIDJSON error
As i tested the file in http://jsonviewer.stack.hu/ it was not error , now i don't know why that error appear , probably @SReject has a better solution about it.
Also https://codebeautify.org/jsonvalidator said it is valid too..
I am curious too about that , so i re-opened just in case it is an issue..
@westor7 ok I think the error is happening because the json file is not formatted correctly. When I used jsonviewer.stack.hu to format the data and save that into a test.json file then your code works beautifully.
So now it's a matter of formatting the string into json format.
@westor7 ok I found the cause.
I noticed that the now_playing.json file is being encoded as UTF-8 with BOM. When I copied the exact json content, ie the entire string in one line, and saved it to a new temp.json file with UTF-8 encoding and ran your script then it worked as expected.
According to this post
So now the question is, how to convert to UTF-8 without BOM in the mIRC script?
I fixed the issue and your code works. I needed to select the encoding from the plugin to UTF-8 instead of UTF-8 with header.
But @SReject might suggest a way to remove the header from file in case someone else needs the functionality.
Thanks for all your help.
I have a json file at c:\temp\nowplaying\nowplaying.json
It contains the following:
{ "nowplaying": { "playing": 1, "paused": 0, "albumartist": "Awesome Artist", "album": "soundcloud.com/thisGuy", "artist": "Awesome Artist", "title": "More please", "tracknumber": 0, "length": 393, "elapsed": 0}
How can I read that json file?
This is what I have so far