我正在尝试使用 datastax java 驱动程序并将该行检索为 JSON。
我做经典
SELECT JSON * from myTable WHERE id=1
,这会在 CQL 上返回一个 Json 格式的字符串。
例如 { "uuid": "12324567-...."}
这有效。
现在,我尝试使用 Java 驱动程序做同样的事情,我使用(在 scala 中)
val resultSet = session.execute(queryString)
我使用以下方法从此结果集中选取一行:"resultSet.one()"
。
这有我需要的字符串,但我该如何获取它?
实验:resultSet.one().getColumnDefinitions.toString
打印:列[ [json] (varchar) ]
实验:resultSet.one().toString()
打印:Row[{"uuid": "3ce19e07-2280-4b31-9475-992bda608e70"}]
<- 我需要的字符串
如何在我的程序中选取一个表示 JSON 的简单字符串,而不尝试拆分上面的字符串?
最佳答案
如 Cassandra documentation 中所述:
The results for
SELECT JSON
will only include a single column named[json]
. This column will contain the same JSON-encoded map representation of a row that is used forINSERT JSON
.
为了访问返回行的 JSON 值,您需要使用定义在 Row
上的 getString
方法之一。类以通过索引或名称获取此列的值:
Row row = resultSet.one();
String json1 = row.getString(0);
String json2 = row.getString("[json]");
关于java - 使用 Datastax Java 驱动程序以 JSON 形式查询行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521108/