简体   繁体   中英

KRL: Parsing string as JSON

After using http:get() , I receive back a string from pick ing the "content" from the hash:

response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content");

However, since json_resp is a string and not an actual JSON object, I can't run a command like this:

value = json_resp.pick("$..string");

Is there a way to tell KRL that I want to parse json_resp as JSON? An eval() or something, perhaps?

The decode() operator does just what you want. It operates on a JSON string, attempting to convert it to a native KRL object. Note that KRL also has encode() which operates on a native KRL object and returns a JSON string representation of that object.

response = http:get(webservice_url, {"key1": value1, "key2": value2});
json_resp = response.pick("$..content").decode();
value = json_resp.pick("$..string");
// will work since json_resp is now a native KRL object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM