Crafting json http respinse

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Crafting json http respinse

Post by jassing » 11 Dec 2019 21:45

This code fails, resulting in "{error}" being delivered as the response.
Screenshot_20191211-001846.png
Screenshot_20191211-001846.png (144.66 KiB) Viewed 9399 times
What is the preferred way to deliver the expected results?

Thanks.

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

Re: Crafting json http respinse

Post by Desmanto » 12 Dec 2019 06:33

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.

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);
Use {json} in the write http response.
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.

User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Re: Crafting json http respinse

Post by jassing » 12 Dec 2019 07:51

Thank you very much...
I was trying to return json-formatted text via write HTTP Response Text...
I really appreciate the example and explanation...

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: Crafting json http respinse

Post by Micky Micky » 12 Dec 2019 19:24

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
Crude but it works.

User avatar
jassing
Posts: 94
Joined: 16 Jul 2017 01:42
Location: SF Bay Area

Re: Crafting json http respinse

Post by jassing » 13 Dec 2019 00:08

Wild... I don't think I would ever have guessed that... Good job. Thanks for sharing.

Post Reply