This code fails, resulting in "{error}" being delivered as the response.
Thanks.
What is the preferred way to deliver the expected results? Crafting json http respinse
Moderator: Martin
Re: Crafting json http respinse
Creating JSON directly from string is very challenging. You have to use single quote to quote the element, since double quote will try to parse the braces into variable. Many problems happen when you try to use inline expression. You also need several string concatenation and makes it difficult to read.
Although it is possible and maybe practical if the JSON is short; but just make it a good practice to create the object first in nested map/list. Later use toJSON() to convert it to JSON. It is clean, and you don't have to deal with the problem above.
I can't see your whole JSON there, so I will try to mimic the best. Put below into script before the response.
Use {json} in the write http response.
Although it is possible and maybe practical if the JSON is short; but just make it a good practice to create the object first in nested map/list. Later use toJSON() to convert it to JSON. It is clean, and you don't have to deal with the problem above.
I can't see your whole JSON there, so I will try to mimic the best. Put below into script before the response.
Code: Select all
obj = newMapFromValues(
"Date", dateymd,
"Times", newMapFromValues(
"AstronomicalDawn", "{Astronomical_Dawn,dateformat,HH:mm:ss}",
"NauticalDawn", "{Nautical_Dawn,dateformat,HH:mm:ss}",
"CivilDawn", "{Civil_dawn,dateformat,HH:mm:ss}",
"Sunrise", "{Sunrise,dateformat,HH:mm:ss}",
"Solarnoon", "{Solarnoon,dateformat,HH:mm:ss}",
"Sunset", "{Sunset,dateformat,HH:mm:ss}",
"CivilDusk", "{Civil_Dusk,dateformat,HH:mm:ss}",
"NauticalDusk", "{Nautical_Dusk,dateformat,HH:mm:ss}",
"AstronomicalDusk", "{Astronomical_Dusk,dateformat,HH:mm:ss}"
),
"LengthOfDay", "{getDurationString(Sunset - Sunrise)}"
);
json = toJSON(obj);
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.
Re: Crafting json http respinse
Thank you very much...
I was trying to return json-formatted text via write HTTP Response Text...
I really appreciate the example and explanation...
I was trying to return json-formatted text via write HTTP Response Text...
I really appreciate the example and explanation...
-
- Posts: 179
- Joined: 16 Oct 2019 17:38
Re: Crafting json http respinse
I did this today and it took ages to figure that it was the [ ] brackets that were needed.
Might be of some help.
My cut and paste has made this look a bit odd. It's one long string without big spaces.
peopleList =
'{"people" :[{"name":"Micky","phone":"012345678","birthday":"0101"},{"name":"Mickey","phone":"12345678","birthday":"0202"},{"name":"Michael ","phone":"2345678","birthday":"0303"},{"name":"Micheal","phone":"345678","birthday":"0404"},{"name":"Mike","phone":"567890","birthday":"0505"}]}';
js = fromJSON (peopleList);
len = length (js["people"]);
name = js["people"][a]["name"];
phone = js["people"][a]["phone"];
birthday = js["people"][a]["birthday"];
a is the index
Might be of some help.
My cut and paste has made this look a bit odd. It's one long string without big spaces.
peopleList =
'{"people" :[{"name":"Micky","phone":"012345678","birthday":"0101"},{"name":"Mickey","phone":"12345678","birthday":"0202"},{"name":"Michael ","phone":"2345678","birthday":"0303"},{"name":"Micheal","phone":"345678","birthday":"0404"},{"name":"Mike","phone":"567890","birthday":"0505"}]}';
js = fromJSON (peopleList);
len = length (js["people"]);
name = js["people"][a]["name"];
phone = js["people"][a]["phone"];
birthday = js["people"][a]["birthday"];
a is the index
Crude but it works.
Re: Crafting json http respinse
Wild... I don't think I would ever have guessed that... Good job. Thanks for sharing.