Reformat writable file

Post your questions and help other users.

Moderator: Martin

Post Reply
Lucy
Posts: 161
Joined: 10 Aug 2019 11:24

Reformat writable file

Post by Lucy » 01 Dec 2019 03:59

Hey i have one functioning api... it is for sun times. The result comes in one line rather than multi. Is it possible to seperate it? Below is a copy of my result but in seperate lines as i would like them to be.


//{"results":{
//"sunrise":"6:48:50 PM",
//"sunset":"9:14:12 AM",
//"solar_noon":"2:01:31 AM",
//"day_length":"14:25:22",
//"civil_twilight_begin":"6:19:18 PM",
//"civil_twilight_end":"9:43:44 AM",
//"nautical_twilight_begin":"5:42:44 PM",
//"nautical_twilight_end":"10:20:18 AM",
//"astronomical_twilight_begin":"5:02:31 PM",
//"astronomical_twilight_end":"11:00:31 AM"},
//"status":"OK"}

Is this achievable?


I also need to alter times but i think i can manage that!!?! 😊

Horschte
Posts: 40
Joined: 03 Nov 2014 18:00

Re: Reformat writable file

Post by Horschte » 01 Dec 2019 13:43

Convert your response to an object and access the desired value like this:

Code: Select all

obj = fromJSON(response);
sunset = obj['results']['sunset'];

Lucy
Posts: 161
Joined: 10 Aug 2019 11:24

Re: Reformat writable file

Post by Lucy » 06 Dec 2019 02:07

Hey ive tried using that method on this but i could not put it in to onject?!?!


{"location":{"name":"xxxxxx","region":"Victoria","country":"xxxxxxxx","lat":xxxxxx,"lon":xxxxxx"tz_id":"xxxxxxxx","localtime_epoch":1575597511,"localtime":"2019-12-06 12:58"},"current":{"last_updated":"2019-12-06 12:45","temp_c":22.0,"condition":{"text":"Sunny"},"wind_kph":24.1,"wind_degree":270,"wind_dir":"W","pressure_in":30.3,"precip_mm":0.0,"humidity":31,"cloud":0,"feelslike_c":23.4,"vis_km":10.0,"uv":6.0,"gust_kph":25.9},"forecast":{"forecastday":[{"date":"2019-12-06","day":{"maxtemp_c":24.9,"mintemp_c":9.2,"avgtemp_c":17.8,"maxwind_kph":23.8,"totalprecip_mm":0.0,"avgvis_km":10.0,"avghumidity":53.0,"condition":{"text":"Partly cloudy"},"uv":10.9},"astro":{"sunrise":"05:49 AM","sunset":"08:18 PM","moonrise":"02:27 PM","moonset":"02:32 AM"}}]},"alert":{}}


I also tried to convert this one too but could not?!?!

{"location":{"name":"xxxxxx","region":"xxxxxx","country":"xxxxxxx,"lat":xxxxxx,"lon":xxxxxx,"tz_id":"xxxxxxxx","localtime_epoch":1575597511,"localtime":"2019-12-06 12:58"},"astronomy":{"astro":{"sunrise":"05:49 AM","sunset":"08:18 PM","moonrise":"02:27 PM","moonset":"02:32 AM"}}}

I have SUCCESSFULLY converted some others but im stuck with these two.

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

Re: Reformat writable file

Post by Desmanto » 06 Dec 2019 10:53

When you try to censor your location, you've deleted the comma. For the first, it is at the "lon":xxxxxx"tz_id". It should be "lon":xxxxxx,"tz_id"
Or when you execute fromJSON(), you should see the error at index 96 of the json string.

For the second one, it is at index 68, "country":"xxxxxxx,"lat". The closing double quotes is missing, should be "country":"xxxxxxx","lat".

These are the fixed version of your json.

Code: Select all

a = '{"location":{"name":"xxxxxx","region":"Victoria","country":"xxxxxxxx","lat":xxxxxx,"lon":xxxxxx,"tz_id":"xxxxxxxx","localtime_epoch":1575597511,"localtime":"2019-12-06 12:58"},"current":{"last_updated":"2019-12-06 12:45","temp_c":22.0,"condition":{"text":"Sunny"},"wind_kph":24.1,"wind_degree":270,"wind_dir":"W","pressure_in":30.3,"precip_mm":0.0,"humidity":31,"cloud":0,"feelslike_c":23.4,"vis_km":10.0,"uv":6.0,"gust_kph":25.9},"forecast":{"forecastday":[{"date":"2019-12-06","day":{"maxtemp_c":24.9,"mintemp_c":9.2,"avgtemp_c":17.8,"maxwind_kph":23.8,"totalprecip_mm":0.0,"avgvis_km":10.0,"avghumidity":53.0,"condition":{"text":"Partly cloudy"},"uv":10.9},"astro":{"sunrise":"05:49 AM","sunset":"08:18 PM","moonrise":"02:27 PM","moonset":"02:32 AM"}}]},"alert":{}}';

b = '{"location":{"name":"xxxxxx","region":"xxxxxx","country":"xxxxxxx","lat":xxxxxx,"lon":xxxxxx,"tz_id":"xxxxxxxx","localtime_epoch":1575597511,"localtime":"2019-12-06 12:58"},"astronomy":{"astro":{"sunrise":"05:49 AM","sunset":"08:18 PM","moonrise":"02:27 PM","moonset":"02:32 AM"}}}';

jsa = fromJSON(a);
jsb = fromJSON(b);
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

Lucy
Posts: 161
Joined: 10 Aug 2019 11:24

Re: Reformat writable file

Post by Lucy » 06 Dec 2019 12:16

Lol... yep. How you said is how the original json is wrote. I was just putting a bunch of "x" in that cause i didnt want to be banned for providing realtime info, etc. On the original output the comma is in its right place. And same with the other one. Sorry i rushed that. All is formatted right as it comes. I just cannot filter certain sections to save as var nor place in to objects. It does not work!?

Lucy
Posts: 161
Joined: 10 Aug 2019 11:24

Re: Reformat writable file

Post by Lucy » 06 Dec 2019 15:48

This is the objects elements as described by the dev:

///

forecastday:
Parent element forecastday
-> day: 'day' element inside forecastday contains
max/min temperature, average temperature

forecastday -> astro forecastday -> hour:

Forecastday
Parent element

forecastday -> dayday element contains:
Max, min and average temperature Max wind speed Total precipitation Day weather condition

forecastday -> astroastro element contains
sunrise, sunset, moonrise and moonset data

forecastday -> hourhour element contains
hour by hour weather forecast information
///

I have tried a few different variants of this:

Code: Select all



obj = fromJSON(response);
one = obj['forecastday']['maxtemp_c'];
obj2 = fromJSON(response);
two = obj2['forecastday']['astro']['sunrise'];
obj3 = fromJSON(response);
three = obj3['forecastday']['condition'];


But it errors with null no matter how i try to grab the elements. I jave no idea what i am doing clearly

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

Re: Reformat writable file

Post by Desmanto » 07 Dec 2019 02:13

No problem with the replacing xxxx, it should be done to protect your privacy too.

You don't need to convert fromJSON() everytime. You only need to convert once and use that object further. Use debug dialog to dig down the obj, to find the key-value you want to access. It is the fastest way to understand how nested map/list work. See how to use debug dialog here : viewtopic.php?t=7262

- The first level of the obj is a Map (look at the top title when you tap the {obj}) contains 4 keys : location, current, forecast, alert. The data you want is in forecast, dig down on ["forecast"]
- It shows you only single key : forecastday, so no other way, dig down on ["forecastday"]
- Now, pay attention to the type above, it now shows List. And it has only single element, with index 0. Dig down on [0]
- Inside the [0], it changes to Map again, with 3 keys : date, day, astro. maxtemp_c is at the day, so dig down on ["day"]
- Finally we see the maxtemp_c directly with the value at the side. You can make sure of this by tapping change value on maxtemp_c to ensure it is not a list/map anymore. It is a number type variable. So the last key is ["maxtemp_c"]

So to get the {maxtemp_c}, we have to dig down very far.

Code: Select all

obj = fromJSON(response);
maxtemp_c = obj["forecast"]["forecastday"][0]["day"]["maxtemp_c"];
You might think it is very long. But once you get used to it, you can conquer any nested map/list object. It is just a bit longer in the script. But eventually you will love it.

Using the same method, you will get the sunrise (in astro key) and condition (dig down another level)

Code: Select all

obj = fromJSON(response);
maxtemp_c = obj["forecast"]["forecastday"][0]["day"]["maxtemp_c"];
sunrise = obj["forecast"]["forecastday"][0]["astro"]["sunrise"];
condition = obj["forecast"]["forecastday"][0]["day"]["condition"]["text"];
Last edited by Desmanto on 07 Dec 2019 03:52, edited 1 time in total.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), Resurrection Remix Official 7.0.2 20190829, Pie 9.0, Rooted.

Lucy
Posts: 161
Joined: 10 Aug 2019 11:24

Re: Reformat writable file

Post by Lucy » 07 Dec 2019 02:52

Oh cool. So i was on the right track when i started using more than one key, just didnt do enough.
Thank you soooooo much. That little walkthrough of my data helped me wrap my head around maps,keys,etc soooooo much better than any tutorial or examples i have read. Omg thank you

Post Reply