简体   繁体   English

在clojure中将json对象转换为字符串

[英]converting json object to string in clojure

I am trying to display a json object from a table to UI screen using clojure,sql korma and angularJS.我正在尝试使用 clojure、sql korma 和 angularJS 将 json 对象从表显示到 UI 屏幕。 I have a table with a column's data type as json.The data base is postgres.When I am trying to run the code, I am getting the an error.我有一个列的数据类型为 json 的表。数据库是 postgres。当我尝试运行代码时,出现错误。 My code to query the DB is below.我查询数据库的代码如下。


  
 
  
  
  
(ns error_api_transactions.models.bre_dve_errors_api_transactions
  (:require [debug.logger :as logger])
  (:use [korma.core]
        [core.config.db]
        [utils.gen_password]
        [core.file-store]
        [utils.uuid :as utils-uuid]))


(defentity bre_errors
  (pk :id)
  (table :bre_errors)
  (database master-db))

(defentity dve_errors
  (pk :id)
  (table :dve_errors)
  (database master-db))

 (defentity vendor_detail
  (pk :id)
  (table :vendor)
  (database master-db))


    (defn get-all-bre-errors
       ^{:Comments ""}
       []
      

      
       
       (select bre_errors
                       (fields [:vendor_id :vendor_id]
                               [:error_json]
                               [:error_xml :error_xml]
                               [:input :input]
                               [:created_on :created_on]
                               [:updated_on :updated_on]
                               [:deleted_on :deleted_on]
                               [:po_number :po_number]
                               [:purchase_order_id :purchase_order_i])
                       )

      )

Error is: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class org.postgresql.util.PGobject: ["Container Numbers in all nodes must be consistent"]错误是:com.fasterxml.jackson.core.JsonGenerationException:无法 JSON 编码类的对象:类 org.postgresql.util.PGobject:[“所有节点中的容器编号必须一致”]

I do not know if we can use any toString methods of java here to enter code here convert JSON to normal string.我不知道我们是否可以在这里使用java的任何toString方法在这里enter code here将JSON转换为普通字符串。 Any help is highly appreciated非常感谢任何帮助

As the datatype of :error_json column is "json" in postgres database so you have to typecast it into "string" from "PGobject json".由于 :error_json 列的数据类型在 postgres 数据库中为“json”,因此您必须将其从“PGobject json”类型转换为“string”。

Yes you can use toString to change the type.是的,您可以使用 toString 来更改类型。

below is the code snippet:下面是代码片段:

(map (fn [value] (update-in value [:error_json] #(.toString %))) (get-all-bre-errors))  

As an alternative to modifying the application code, you can modify your SQL query, like this:作为修改应用程序代码的替代方法,您可以修改 SQL 查询,如下所示:

SELECT id, jsonbValue::TEXT
FROM myTable

Adding ::TEXT will cast the jsonb to text.添加::TEXT会将 jsonb 转换为文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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