Your Universal Remote Control Center
RemoteCentral.com
Philips Pronto Professional Forum - View Post
Previous section Next section Previous page Next page Up level
Up level
The following page was printed from RemoteCentral.com:

Login:
Pass:
 
 

Page 2 of 2
Topic:
JSON.parse()
This thread has 22 replies. Displaying posts 16 through 23.
Post 16 made on Wednesday June 24, 2020 at 15:17
thebaz67
Long Time Member
Joined:
Posts:
December 2008
13
I was just attempting to work on one of the weather modules to see if I could use openweather to get the feed that is required but I am lost trying to map the xml data properly.

I am interested in the project as well.

thanks
Basil
Post 17 made on Wednesday June 24, 2020 at 18:52
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
Email me at the address on my rc profile please.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 18 made on Friday September 4, 2020 at 04:47
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
Hi,
I have a HomeSeer json request and the response is:

Name"HomeSeer Devices"
Version"1.0"
Devices
0
ref2011
name"YR Wetter für Pronto link Text"
location"Jon00 DataScraper"
location2"YR Wetter current"
value40
status"40"
device_type_string""
last_change"/Date(1599204401437+0200)/"
relationship0
hide_from_viewfalse
associated_devices[]
device_type
Device_API0
Device_API_Description"No API"
Device_Type0
Device_Type_Description"Type 0"
Device_SubType0
Device_SubType_Description""
device_type_valuesnull
UserNote""
UserAccess"Any"
status_image""
voice_command""
misc4352
interface_name""

Here are the raw data:

{"Name":"HomeSeer Devices","Version":"1.0","Devices":[{"ref":2011,"name":"YR Wetter für Pronto link Text","location":"Jon00 DataScraper","location2":"YR Wetter current","value":40.0,"status":"40","device_type_string":"","last_change":"\/Date(1599204401437+0200)\/","relationship":0,"hide_from_view":false,"associated_devices":[],"device_type":{"Device_API":0,"Device_API_Description":"No API","Device_Type":0,"Device_Type_Description":"Type 0","Device_SubType":0,"Device_SubType_Description":""},"device_type_values":null,"UserNote":"","UserAccess":"Any","status_image":"","voice_command":"","misc":4352,"interface_name":""}]}


I want to parse the value  (in that case 40)

How to do this ?

Regards,
Fischi
Post 19 made on Friday September 4, 2020 at 20:09
mpg7321
Regular Member
Joined:
Posts:
June 2020
109
The quick and dirty way, I assume you have the returned data in a varable.

var returneddata = socket.read();
var value = returneddata.split('"value":').pop().split('.')[0];

That should give you 40 not 40.0, if you want 40.0 try,

var value = returneddata.split('"value":').pop().split(',"status"')[0];

This has worked for me with KODI that uses Json.
Post 20 made on Saturday September 5, 2020 at 11:05
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
Fischi,

You could go here:

[Link: github.com]

Get this file and make a Prontoscript library called json2.js (add the necessary @title, @version, @author comments to the top of it). The file, with comments is like 18KB. If you strip out the comments you can cut down the size and load time on the Pronto tremendously. In fact, I have the original version but the one I use is an optimized one that I ran through the Google Closure compiler.


What will require some further research is whether the above library will fully support the Date markup above noted as:

"\/Date(1599204401437+0200)\/"

It does appear in the markup that the value above is wrapped in quotes which means it will be parsed as a string. If you need to get the real date, it will require a bit of coding to recreate it. It looks as if it is the number or seconds or milliseconds since Jan 01, 1970 with a +2 hour offset from UTC.

Next, you have "YR Wetter für Pronto link Text". A second research topic.

Because of these characters which are above ASCII position 127, the entire string may need to be UTF-8 decoded prior to executing Parse. There are various UTF-8 decoders in pure javascript that you could create a library from as well.



Then once adding the library to your Activity you could simply do.

var obj = JSON.parse(responseText);
var myValue = obj.Devices[0].value;

Again, this is my opinion but the above 2 lines are much more readable than the string munging that was suggested in the previous post.

Finally, if you need to send a request, simply build it as an object and 'stringify' it.

var myRequest = {foo:"one",bar:2};
// using a TCP socket as an example of a transport.
mySocket.write(JSON.stringify(myRequest));

Last edited by Lyndel McGee on September 5, 2020 11:22.
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 21 made on Sunday September 6, 2020 at 14:57
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
Fischi,

From this post, I see you already have json2 loaded as a library.

[Link: remotecentral.com]

Are you having some sort of specific error or were asking how to reference the value in the parsed object?

Here's how you would get the value you were asking for.

var obj = JSON.parse(responseText);
var myValue = obj.Devices[0].value;

Thanks,
Lyndel
Lyndel McGee
Philips Pronto Addict/Beta Tester
Post 22 made on Tuesday October 13, 2020 at 15:07
Fischi
Long Time Member
Joined:
Posts:
January 2011
114
Hi,
I've made good progress with Pronto and json.

One last problem:

I want to change a widget depending on a value change.

var value4 = jsonObj.Devices[4].value;
{ widget("DEV4").setImage(CF.widget(value4, "RECOURCES").getImage(0),0);
return; }

I get the message in the console :

ProntoScript error: Argument is not a string
Offending page script: Tag: 'HOME'
Offending line #190: "{ widget("DEV4").setImage(CF.widget(value4, "RECOURCES").getImage(0),0);"

Where is the issue ?

Regards, Fischi
Post 23 made on Wednesday October 14, 2020 at 16:43
Lyndel McGee
RC Moderator
Joined:
Posts:
August 2001
12,992
The issue was that value4 was a number and not a string. CF.widget requires that all tags are strings.

The solution was:

widget("DEV4").setImage(CF.widget("" + value4, "RECOURCES").getImage(0),0);
Lyndel McGee
Philips Pronto Addict/Beta Tester
Page 2 of 2


Jump to


Protected Feature Before you can reply to a message...
You must first register for a Remote Central user account - it's fast and free! Or, if you already have an account, please login now.

Please read the following: Unsolicited commercial advertisements are absolutely not permitted on this forum. Other private buy & sell messages should be posted to our Marketplace. For information on how to advertise your service or product click here. Remote Central reserves the right to remove or modify any post that is deemed inappropriate.

Hosting Services by ipHouse