Newtonsoft.JSON抛出SerializationException

ct2axkht  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(121)

我在使用Newtonsoft.JSON包将一些JSON数据从Drupal站点反序列化到C#应用程序时遇到了问题。
在运行这行代码时(其中.equipment是Equipment类型的对象):

App.cmSession.IUSession.equipment = JsonConvert.DeserializeObject<Equipment>(equipJSON);

抛出此错误:
抛出的异常:Newtonsoft.Json.dll中的“Newtonsoft.Json.JsonSerializationException”检索设备数据失败:无法反序列化当前JSON对象(例如{“name”:“value”})转换为类型'myProject.Classes.User_Data.Objects.DrupalObject[]',因为该类型需要JSON数组(例如,[1,2,3])以正确地反序列化。要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化的类型,使其成为普通的.NET类型(例如不是integer之类的原始类型,也不是array或List之类的集合类型)。还可以将JsonObjectAttribute添加到类型中,以强制它从JSON对象反序列化。路径'data[0].relationships.field_equipment_area. data',第1行,位置5372。
我查看了其他几个问题,但它们并没有直接帮助我解决问题:Newtonsoft JsonSerializationExceptionCannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctlyJsonSerializationException
其他注意事项:

  • 我不能改变我的JSON结构,它是第三方的。
  • Taxonomy对象在自己序列化时可以成功序列化,因此我怀疑DrupalObject和Taxonomy的编码是正确的。
  • Taxonomy是一个纯粹为了避免代码混乱而创建的类,它没有扩展其他功能。
  • Equipment扩展了DrupalObject,以从JSON中添加一些需要序列化的DrupalObject所独有的附加属性。
  • 我尝试将Equipment中的关系更改为DrupalObject,但这并没有改变任何错误。

也就是说,这里使用了一些类的上下文:

DrupalObject和设备:

public class DrupalObject
    {
        public DrupalData[] data { get; set; }
        public DrupalLinks links { get; set; }

        public DrupalObject() {}
      }

  public class Equipment : DrupalObject
    {
        public new EquipmentData[] data { get; set; }

        public Equipment() {}
    }

DrupalObject和Equipment支持类:

// Classes used for building Equipment (the intent of this is for an Equipment object to contain (and serialize) all fields defined in DrupalObject, as well as the other defined fields in EquipmentAttributes, EquipmentRelationships, etc.

    public class EquipmentData : DrupalData
    {
        public new EquipmentAttributes attributes   { get; set;}
        public new EquipmentRelationships relationships  { get; set; }
    }

    public class EquipmentAttributes : DrupalAttributes
    {
        public int drupal_internal__nid { get; set;  }
        public int drupal_internal__vid { get; set;  }
        public string title { get; set; }
        public string moderation_state { get; set; }
    }

    public class EquipmentRelationships : DrupalRelationships
    {
        public DrupalObject[] field_equipment_area { get; set;   }
        public DrupalObject[] field_equipment_category { get; set;   }
    }

    // Classes for building the DrupalObject

    public class DrupalData
    {
        public string type { get; set; }
        public string id { get; set; }
        public DrupalLinks links { get; set; }
        public DrupalAttributes attributes { get; set; }
        public DrupalRelationships relationships { get; set; }
        public DrupalMeta meta { get; set; }
    }

    public class DrupalAttributes
    {
        public int drupal_internal__tid { get; set;   }
        public bool status { get; set;   }
        public string name { get; set;   }
        public object description { get; set;   }
        public DateTime created { get; set;   }
        public DateTime changed { get; set;   }
    }

    public class DrupalRelationships
    {
        public Taxonomy parent { get; set; }
    }

JSON示例数据(仅限一条):

{
    "jsonapi": {
        "version": "1.0",
        "meta": {
            "links": {
                "self": {
                    "href": "http://jsonapi.org/format/1.0/"
                }
            }
        }
    },
    "data": [
        {
            "type": "node--equipment",
            "id": "2caa087b-949d-455b-a146-a5f31dff5249",
            "links": {
                "self": {
                    "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249?resourceVersion=id%3A970"
                }
            },
            "attributes": {
                "drupal_internal__nid": 415,
                "drupal_internal__vid": 970,
                "langcode": "en",
                "revision_timestamp": "2023-04-25T01:12:32+00:00",
                "revision_log": null,
                "status": true,
                "title": "test23",
                "created": "2023-04-25T01:12:32+00:00",
                "changed": "2023-04-25T01:12:32+00:00",
                "promote": true,
                "sticky": false,
                "default_langcode": true,
                "revision_translation_affected": true,
                "moderation_state": "published",
                "path": {
                    "alias": "/equipment/test23",
                    "pid": 955,
                    "langcode": "en"
                },
                "field_equipment_name": "test2",
            },
            "relationships": {
                "node_type": {
                    "data": {
                        "type": "node_type--node_type",
                        "id": "f330b38f-f414-4689-9b7e-11b119673db5",
                        "meta": {
                            "drupal_internal__target_id": "equipment"
                        }
                    },
                    "links": {
                        "related": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/node_type?resourceVersion=id%3A970"
                        },
                        "self": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/relationships/node_type?resourceVersion=id%3A970"
                        }
                    }
                },
                "revision_uid": {
                    "data": {
                        "type": "user--user",
                        "id": "fad1cbb3-a0f5-4c42-a516-d2a09178723d",
                        "meta": {
                            "drupal_internal__target_id": 3
                        }
                    },
                    "links": {
                        "related": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/revision_uid?resourceVersion=id%3A970"
                        },
                        "self": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/relationships/revision_uid?resourceVersion=id%3A970"
                        }
                    }
                },
                "field_equipment_area": {
                    "data": null,
                    "links": {
                        "related": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/field_equipment_area?resourceVersion=id%3A970"
                        },
                        "self": {
                            "href": "http://mysite.local/jsonapi/node/equipment/2caa087b-949d-455b-a146-a5f31dff5249/relationships/field_equipment_area?resourceVersion=id%3A970"
                        }
                    }
                }
            }
        }
    ],
    "links": {
        "next": {
            "href": "http://mysite.local/jsonapi/node/equipment?page%5Boffset%5D=1&page%5Blimit%5D=1"
        },
        "self": {
            "href": "http://mysite.local/jsonapi/node/equipment?page%5Blimit%5D=1&page%5Boffset%5D=0"
        }
    }
}

我还应该注意到,如果没有任何东西与之关联,field_equipment_area有时会有空值,这是正常的。否则它看起来会像这样:

"field_equipment_area": {
                "data": {
                    "type": "taxonomy_term--equipment_areas",
                    "id": "497316c8-183c-49d2-a994-d2bcd6ddabe6",
                    "meta": {
                        "drupal_internal__target_id": 542
                    }
frebpwbc

frebpwbc1#

从代码中很难理解类是如何被依赖的。在我看来,类和json strig是完全不同的。这个代码可以工作。您可以添加“Drupal”前缀到您想要的类和属性

JsonConvert.DeserializeObject<Equipment>(json);

public class Equipment
{
    public Jsonapi jsonapi { get; set; }
    public List<Data> data { get; set; }
    public Links links { get; set; }
}
public class Attributes
{
    public int drupal_internal__nid { get; set; }
    public int drupal_internal__vid { get; set; }
    public string langcode { get; set; }
    public DateTime revision_timestamp { get; set; }
    public object revision_log { get; set; }
    public bool status { get; set; }
    public string title { get; set; }
    public DateTime created { get; set; }
    public DateTime changed { get; set; }
    public bool promote { get; set; }
    public bool sticky { get; set; }
    public bool default_langcode { get; set; }
    public bool revision_translation_affected { get; set; }
    public string moderation_state { get; set; }
    public Path path { get; set; }
    public string field_equipment_name { get; set; }
}

public class Data
{
    public string type { get; set; }
    public string id { get; set; }
    public Meta meta { get; set; }
    public Links links { get; set; }
    public Attributes attributes { get; set; }
    public Relationships relationships { get; set; }
}

public class FieldEquipmentArea
{
    public object data { get; set; }
    public Links links { get; set; }
}

public class Jsonapi
{
    public string version { get; set; }
    public Meta meta { get; set; }
}

public class Links
{
    public Self self { get; set; }
    public Related related { get; set; }
    public Next next { get; set; }
}

public class Meta
{
    public Links links { get; set; }
    public string drupal_internal__target_id { get; set; }
}

public class Next
{
    public string href { get; set; }
}

public class NodeType
{
    public Data data { get; set; }
    public Links links { get; set; }
}

public class Path
{
    public string alias { get; set; }
    public int pid { get; set; }
    public string langcode { get; set; }
}

public class Related
{
    public string href { get; set; }
}

public class Relationships
{
    public NodeType node_type { get; set; }
    public RevisionUid revision_uid { get; set; }
    public FieldEquipmentArea field_equipment_area { get; set; }
}

public class RevisionUid
{
    public Data data { get; set; }
    public Links links { get; set; }
}

public class Self
{
    public string href { get; set; }
}

相关问题