I did this once before, but now I can't remember how I did it....
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.
usng string function to extract values
Moderator: Martin
Re: usng string function to extract values
Hi,
Following script should work (please note the single quotes around the strings). This script assumes that the value is stored in variable s.
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:
The first line in the script also needs to add a fake root element so that the created XML is valid.
Regards,
Martin
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 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");
Regards,
Martin
Re: usng string function to extract values
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
I used your first method. It's so simple I'm ashamed I couldn't get it right without help.
Thank you very much
Re: usng string function to extract values
No problem, glad I could help.
Re: usng string function to extract values
Just wanted to jump in to thank Martin. Your solution worked. 2 days trying to find a workable script finally came to an end.
Re: usng string function to extract values
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"];
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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.