【问题标题】:strip identifier from result of .toString() method从 .toString() 方法的结果中去除标识符
【发布时间】:2021-11-13 11:17:02
【问题描述】:

无奈之下,我不懂java。基本上,我想将 couchbase lite Document 解析回 react-native 应用程序,如下所示:

       Document doc = database.getDocumet('id');
       promise.resolve(doc);

不能转换错误,所以我转向 toString(),希望在 react-native 中解析它:

      promise.resolve(doc.toString());

//在js中:

      const document = getNativeDocument('id');
      console.log(document)
      const v = JSON.parse(document);

但这也因错误而失败:

        SyntaxError: JSON Parse error: Unexpected identifier "Document"]. 

控制台结果是:

Document{@0xb3181c18e30a7f1-1f61-49a0-9ca0-1dc3d2e343ed@1-a7ed608736c34cf5860a0d35968e6441e1f089ef(..):obj=>Dictionary{(..)a=>A,b=>B},age=>20,firstPromo=>null}

我尝试了将 Document 转换为可以通过 promise.resolve() 轻松解决的其他时间的解决方案,但现在想知道是否可以使用 .toString() 的结果

【问题讨论】:

    标签: java react-native


    【解决方案1】:

    toString 方法生成用于调试的对象的内部表示,而不是文档的 JSON。您需要使用不同的方法将您的 Document 序列化为 JSON。

    最流行的选择是使用 Jackson 库:https://www.baeldung.com/jackson-object-mapper-tutorial

    如果您将 Spring 框架与 Spring 数据沙发库连接器一起使用,那么 Jackson 的 ObjectMapper 应该已经可以作为 Bean 用于您的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多