KRL: Parsing string as JSON
After using ht开发者_如何学JAVAtp: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
精彩评论