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"];