简体   繁体   English

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

[英]Convert JSON String to C# Object For Complex JSON String

I am trying to convert JSON string to C# Object, but I am getting null values 我正在尝试将JSON字符串转换为C#对象,但是我得到的是空值

Below is the JSON string I have 以下是我拥有的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
    }
]

And the classes for JSON object 和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; }
}

And below code is used to get the object 下面的代码用于获取对象

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

But I am getting, null values in the object as below(open image in new tab if image is not displayed below), 但是我得到的是,对象中的空值如下(如果未在下面显示图像,则在新选项卡中打开图像),

Let me know on the same, thanks 同样让我知道,谢谢

Hadn't made the below properties accessible, below are the changes. 无法访问以下属性,以下是更改。 Thanks to @John. 感谢@John。

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

You may give default value to them like : 您可以为它们提供默认值,例如:

public int X { get; set; } = 0;
public string Y { get; set; } = "";

Your class looks like not match the format of JSON string, try this 您的课程看起来与JSON字符串的格式不匹配,请尝试以下操作

  1. add a new class 添加一个新的班级
  2. remove the part of class "public class Class1{}" 删除类“公共类Class1 {}”的一部分
  3. copy the JSON string 复制JSON字符串
  4. Edit -> Paste Special -> Paste JSON as Classes 编辑->选择性粘贴->将JSON作为类粘贴

then JSON.Net will match the Object and JSON string 然后JSON.Net将匹配Object和JSON字符串

I have tried your sample JSON string and the object is created by step 4 like this 我已经尝试过您的示例JSON字符串,并且像这样通过第4步创建了对象

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string extraction_method { get; set; }
    public float top { get; set; }
    public float left { get; set; }
    public float width { get; set; }
    public float height { get; set; }
    public Datum[][] data { get; set; }
    public int spec_index { get; set; }
}

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

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

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