首页 > 解决方案 > NewtonSoft 反序列化嵌套的 JSON Blue Prism

问题描述

我正在使用 Blue Prism 调用 Microsoft 计算机视觉 API 来识别 PDF 中的文本。

获得 JSON 响应后,Blue Prism 使用 Blue Prism 的 JSON 实用程序或 Microsoft 计算机视觉技能上提供的操作将其转换为集合 (DataTable):获取读取操作响应。

问题:由于数据类型错误,Blue Prism 在某些情况下无法将 JSON 文本解析为集合。

有人可以建议将这种类型的 JSON 结构转换为 Blue Prism 可以读取的 DataTable 的代码吗?

我附上了 2 个 JSON 的 pastebin 链接(正文限制为 30000 个字符,不能在此处复制它们)。

在 Blue Prism 中可读(包含特殊字符 !"#$%&/()"'):

https://pastebin.com/QMPCiQty

在 Blue Prism 中不可读:

https://pastebin.com/V3HVssQz

Blue Prism 给出以下错误:

使用“获取读取操作响应”操作:

内部:意外错误数组中的数据类型不匹配:元素“3”具有类型而不是预期类型“System.Double”

使用“Blue Prism 的 JSON 实用程序”:

内部:无法执行代码阶段,因为代码阶段抛出异常:数组中的数据类型不匹配

我尝试通过使用 json2csharp.com 提供的工具创建公共类来将嵌套的 JSON 解析为数据表,然后指向它们并写入数据表。我还尝试不创建类,只需通过指向属性recognitionResults将 JSON 直接解析到数据表。

没有类:

数据集 ds = JObject.Parse(json_txt)["recognitionResults"].ToObject();

与类:

DataTable dt = (DataTable)JsonConvert.DeserializeObject(json_txt, (typeof(DataTable)));

课程:

public class Word
{
    public List<double> boundingBox { get; set; }
    public string text { get; set; }
    public string confidence { get; set; }
}

public class Line
{
    public List<double> boundingBox { get; set; }
    public string text { get; set; }
    public List<Word> words { get; set; }
}

public class RecognitionResult
{
    public int page { get; set; }
    public double clockwiseOrientation { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public string unit { get; set; }
    public List<Line> lines { get; set; }
}

public class RootObject
{
    public string status { get; set; }
    public List<RecognitionResult> recognitionResults { get; set; }
}

以下是 Blue Prism 中嵌套集合的外观: Blue Prism Nested Response Collection

标签: c#jsonnestedocrblueprism

解决方案


如果您使用的是版本 6 或更高版本,您应该看看 Blue Prism DX,这是社区的一个部门,用户可以在其中交换 VBO 和知识。这个问题已经解决了:

JSON 实用程序蓝色棱镜

此实用程序提供将 JSON 解析为集合以及将集合转换为 JSON 的功能。


推荐阅读