usng string function to extract values

Post your questions and help other users.

Moderator: Martin

Post Reply
vfn
Posts: 4
Joined: 15 Feb 2014 01:43

usng string function to extract values

Post by vfn » 28 May 2014 23:13

I did this once before, but now I can't remember how I did it.... :oops:

I get a string of data via http request. It looks like this:

{"temp":79.00,"tmode":2,"fmode":0,"override":1,"hold":0,"t_cool":76.00,"tstate":2,"fstate":1,"time":{"day":2,"hour":9,"minute":35},"t_type_post":0}

(it's from the thermostats in my house)

I can get the data and save it to a file or variable no problem.

I want to extract the numeric value that follows temp and t_cool and write them to a variable to be used in the next step.

I know I can do it with "substring" but I just can't get the syntax right.

Please help.

Thanks in advance.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: usng string function to extract values

Post by Martin » 29 May 2014 15:30

Hi,

Following script should work (please note the single quotes around the strings). This script assumes that the value is stored in variable s.

Code: Select all

start = indexOf(s, '"temp":')+7;
end = indexOf(s, ',', start);
v = substring(s, start, end);
The first line searches the index of the text "temp": and adds 7 characters to skip the "temp":.
The second line searches for the first comma after start.
The third line extracts the value 79.00 from the original string.

This page contains some scripting examples: Script examples (see example Extract the contents of an HTML element)

You could also use the unofficial JSON to XML converter and extract the values using Xpath:

Code: Select all

xml = convertJSONtoXML('{"root":'+s+"}");
temp = evaluateXPathAsString(xml, "//temp");
t_cool = evaluateXPathAsString(xml, "//t_cool");
The first line in the script also needs to add a fake root element so that the created XML is valid.

Regards,
Martin

vfn
Posts: 4
Joined: 15 Feb 2014 01:43

Re: usng string function to extract values

Post by vfn » 29 May 2014 19:51

Martin,

I used your first method. It's so simple I'm ashamed I couldn't get it right without help.

Thank you very much

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: usng string function to extract values

Post by Martin » 30 May 2014 08:24

No problem, glad I could help.

Rhor
Posts: 5
Joined: 03 Jun 2017 18:00

Re: usng string function to extract values

Post by Rhor » 29 Sep 2019 22:46

Just wanted to jump in to thank Martin. Your solution worked. 2 days trying to find a workable script finally came to an end.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: usng string function to extract values

Post by Desmanto » 01 Oct 2019 17:22

Rhor wrote:
29 Sep 2019 22:46
Just wanted to jump in to thank Martin. Your solution worked. 2 days trying to find a workable script finally came to an end.
Although you might have solved it, but the solution is from old version. Now we have fromJSON() function already, and you should use that to be more efficient.

Code: Select all

response = '{"temp":79.00,"tmode":2,"fmode":0,"override":1,"hold":0,"t_cool":76.00,"tstate":2,"fstate":1,"time":{"day":2,"hour":9,"minute":35},"t_type_post":0}';

js = fromJSON(response);

temp = js["temp"];
t_cool = js["t_cool"];
Accessing the temp and t_cool is much easier, simply point to the key of the converted json. Use Condition debug dialog after the script to check the result.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply