如何将JSON响应读取到C#中的文档AI的文档对象

发布于 2025-01-25 15:29:35 字数 863 浏览 5 评论 0原文

我正在尝试从云存储中读取JSON文件,并尝试将其转换为Google.cloud.documentai.v1.document。

我已经完成了POC,但是它的投掷异常google.protobuf.invalidprotocolbufferexception:“协议消息端组标签与预期标签不匹配。”

首先,我将.json文件读取到MemoryStream中,并尝试合并到文档中。

  using Google.Cloud.DocumentAI.V1;

  public static void StreamToDocument()
    {
        byte[] fileContents = File.ReadAllBytes("C:\\upload\\temp.json");
        
        using (Stream memoryStream = new MemoryStream(fileContents))
        {
            Document doc = new Document();
            var byteArray = memoryStream;
            doc.MergeFrom(byteArray);
        }           
    }

错误消息我正在得到的是

”在此处输入图像描述”

我还有其他方法可以实现吗?

I am trying to read json file from cloud storage and trying to convert that into Google.Cloud.DocumentAI.V1.Document.

I have done POC, but its throwing exception Google.Protobuf.InvalidProtocolBufferException: 'Protocol message end-group tag did not match expected tag.'

First I am reading .Json file into MemoryStream and trying to Merge in to Document.

  using Google.Cloud.DocumentAI.V1;

  public static void StreamToDocument()
    {
        byte[] fileContents = File.ReadAllBytes("C:\\upload\\temp.json");
        
        using (Stream memoryStream = new MemoryStream(fileContents))
        {
            Document doc = new Document();
            var byteArray = memoryStream;
            doc.MergeFrom(byteArray);
        }           
    }

Error Message I am getting is

enter image description here

Is there any other way I can achieve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2025-02-01 15:29:35

您指定的代码期望数据为 binary ProtoBuf内容。对于JSON,您想要:

string json = File.ReadAllText("C:\\upload\\temp.json");
Document document = Document.Parser.ParseJson(json);

The code that you've specified there expects the data to be binary protobuf content. For JSON, you want:

string json = File.ReadAllText("C:\\upload\\temp.json");
Document document = Document.Parser.ParseJson(json);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文