c# - 将 JSON 字符串转换为复杂 JSON 字符串的 C# 对象

标签 c# json json-deserialization

我正在尝试将 JSON 字符串转换为 C# 对象,但我得到的是空值

下面是我的JSON字符串

[
    {
        "extraction_method": "stream",
        "top": 0.0,
        "left": 0.0,
        "width": 559.0,
        "height": 732.2100219726562,
        "data": [[
                {
                    "top": 0.0,
                    "left": 0.0,
                    "width": 0.0,
                    "height": 0.0,
                    "text": ""
                },
                {
                    "top": 96.36,
                    "left": 129.27,
                    "width": 102.97900390625,
                    "height": 6.550000190734863,
                    "text": "Sample"
                },
                {
                    "top": 96.35,
                    "left": 311.0,
                    "width": 27.188995361328125,
                    "height": 6.550000190734863,
                    "text": "PT"
                },
                {
                    "top": 96.35,
                    "left": 361.0,
                    "width": 41.248992919921875,
                    "height": 6.550000190734863,
                    "text": "HT"
                },
                {
                    "top": 96.36,
                    "left": 432.11,
                    "width": 28.141387939453125,
                    "height": 6.550000190734863,
                    "text": "RT."
                },
                {
                    "top": 96.36,
                    "left": 480.88,
                    "width": 29.64898681640625,
                    "height": 6.550000190734863,
                    "text": "LT."
                },
                {
                    "top": 96.36,
                    "left": 522.33,
                    "width": 36.660003662109375,
                    "height": 6.550000190734863,
                    "text": "MT"
                }
            ], [
                {
                    "top": 727.57,
                    "left": 75.24,
                    "width": 14.902000427246094,
                    "height": 4.619999885559082,
                    "text": "Tee"
                },
                {
                    "top": 0.0,
                    "left": 0.0,
                    "width": 0.0,
                    "height": 0.0,
                    "text": ""
                },
                {
                    "top": 727.57,
                    "left": 315.0,
                    "width": 14.00201416015625,
                    "height": 4.619999885559082,
                    "text": "IO."
                },
                {
                    "top": 727.59,
                    "left": 381.43,
                    "width": 16.6820068359375,
                    "height": 4.619999885559082,
                    "text": "1.10"
                },
                {
                    "top": 727.59,
                    "left": 434.53,
                    "width": 25.582000732421875,
                    "height": 4.619999885559082,
                    "text": "30.00"
                },
                {
                    "top": 727.59,
                    "left": 488.98,
                    "width": 21.131988525390625,
                    "height": 4.619999885559082,
                    "text": "8.00"
                },
                {
                    "top": 727.59,
                    "left": 534.53,
                    "width": 24.469959259033203,
                    "height": 4.619999885559082,
                    "text": "18.00"
                }
            ]],
        "spec_index": 0
    }
]

以及 JSON 对象的类

public class JsonHelper
{
    string top { get; set; }
    string left { get; set; }
    string width { get; set; }
    string height { get; set; }
    string text { get; set; }
}

public class RootObject
{
    public string extraction_method { get; set; }
    public double top { get; set; }
    public double left { get; set; }
    public double width { get; set; }
    public double height { get; set; }
    public List<List<JsonHelper>> data { get; set; }
    public int spec_index { get; set; }
}

下面的代码用于获取对象

using (StreamReader r = new StreamReader(@"SamplePDF.json"))
        {
            string json = r.ReadToEnd();
            List<RootObject> items = 
                    JsonConvert.DeserializeObject<List<RootObject>>(json);
        }

但是我得到的是对象中的空值,如下所示(如果下面未显示图像,则在新选项卡中打开图像),

请告诉我,谢谢

最佳答案

尚未使以下属性可访问,以下是更改。感谢@John。

public class JsonHelper
{
    string top { get; set; }
    string left { get; set; }
    string width { get; set; }
    string height { get; set; }
    string text { get; set; }
}

关于c# - 将 JSON 字符串转换为复杂 JSON 字符串的 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759727/

相关文章:

c# - 自定义分页问题

c# - 重播功能和参数列表

R 中的 JSON(使用 jsonlite)解析错误

javascript - 引用没有名称的 JSON 数组

java - 如何使用 Jackson 将 Color java 类解析为 JSON?

c# - app.config 从其他节点读取关键连接字符串的值

c# - 为什么我的射弹没有绘制在屏幕上?

c# - 使用 JSON 架构正确创建 .NET 类

java - Jackson json反序列化,根元素来自json

java - 使用 Jackson 将 JSON 字符串转换为 Java 对象