gpt4 book ai didi

c# - 使用 LINQ/C# 使用 JSON 内容

转载 作者:太空狗 更新时间:2023-10-30 01:02:57 26 4
gpt4 key购买 nike

我正在尝试处理大量文件并将它们的数据移动到 SharePoint 列表中。我对我的请求的 SharePoint 组件完全满意,但我正在努力全神贯注于获取 JSON 并针对它执行 C# 语句。

我正在使用的 JSON 采用以下格式:

{
"details": {
"persona": "jdfh sjahfj ashd",
"firstName": "difdfsdfh",
"surname": "safasfjhgfasdf",
"email": "dfashfjsahf@dfjhdfjhsd.com",
"phone": "3256545 421",
"organisation": "dsfhjafd gas gdf",
"department": "hsagfhsdgahjf",
"address": {
"street": "11/338 shjafg ash fg",
"suburb": "gsagfghasf",
"postcode": "4006"
},
"questions": {
"iAmA": "fhsahjfsajhd df as",
"iWorkAtA": "",
"iAmSeekingFor": "ahshfjgsfggdh",
"iAmAMemberOf": "dafjksfhdh",
"furtherInfoOptIn": true,
"newsletterOptIn": false,
"privacyCollection": true
}
},
"orders": [
{
"name": "Business Cards",
"quantity": 8
},
{
"name": "QUITLINE",
"quantity": 5
}
]
}

基于 this StackExchange 请求我尝试创建一个类来将我的内容反序列化为使用以下内容:

var des = (ResourceOrdersJSONEntity)Newtonsoft.Json.JsonConvert.DeserializeObject(sampleJSON, typeof(ResourceOrdersJSONEntity));

这对我不起作用,我怀疑我正在寻找完全错误的路径。我需要学习什么才能获取 JSON 并将 linq 语句包装在它周围以便处理到 SharePoint 中?

我收到的错误是:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

我不太明白它对我意味着什么,我确信这是我的类结构的问题,但我需要一些关于如何解决它的指导。

这是我为反序列化创建的类:

主要实体 公共(public)类 ResourceOrdersJSONEntity { 公共(public)列表详细信息 { get;放; } 公共(public)列表订单 { get;放; }

详情

public class ResourceOrdersDetailsEntity
{
/// <summary>
/// Gets or sets the persona.
/// </summary>
public string Persona { get; set; }

/// <summary>
/// Gets or sets the first name.
/// </summary>
public string FirstName { get; set; }

/// <summary>
/// Gets or sets the surname.
/// </summary>
public string Surname { get; set; }

/// <summary>
/// Gets or sets the email.
/// </summary>
public string Email { get; set; }

/// <summary>
/// Gets or sets the phone.
/// </summary>
public string Phone { get; set; }

/// <summary>
/// Gets or sets the organisation.
/// </summary>
public string Organisation { get; set; }

/// <summary>
/// Gets or sets the department.
/// </summary>
public string Department { get; set; }

/// <summary>
/// Gets or sets the address.
/// </summary>
public ResourceOrdersAddressEntity Address { get; set; }

/// <summary>
/// Gets or sets the questions.
/// </summary>
public ResourceOrdersQuestionsEntity Questions { get; set; }
}

地址实体

   public class ResourceOrdersAddressEntity
{
/// <summary>
/// Gets or sets the street.
/// </summary>
public string Street { get; set; }

/// <summary>
/// Gets or sets the suburb.
/// </summary>
public string Suburb { get; set; }

/// <summary>
/// Gets or sets the postcode.
/// </summary>
public string Postcode { get; set; }
}

问题实体

public class ResourceOrdersQuestionsEntity
{
/// <summary>
/// Gets or sets the i ama.
/// </summary>
public string IAma { get; set; }

/// <summary>
/// Gets or sets the i work at a.
/// </summary>
public string IWorkAtA { get; set; }

/// <summary>
/// Gets or sets the i am seeking for.
/// </summary>
public string IAmSeekingFor { get; set; }

/// <summary>
/// Gets or sets the i am a member for.
/// </summary>
public string IAmAMemberFor { get; set; }

/// <summary>
/// Gets or sets the further info opt in.
/// </summary>
public string FurtherInfoOptIn { get; set; }

/// <summary>
/// Gets or sets the news letter opt in.
/// </summary>
public string NewsLetterOptIn { get; set; }

/// <summary>
/// Gets or sets the privacy collection.
/// </summary>
public string PrivacyCollection { get; set; }
}

订单实体

public class ResourceOrdersQuantityEntity
{
/// <summary>
/// Gets or sets the name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Gets or sets the quantity.
/// </summary>
public int Quantity { get; set; }
}

最佳答案

我只需将 ResourceOrdersJSONEntity 更改为:

即可让您的代码正常工作:
public class ResourceOrdersJSONEntity
{
public ResourceOrdersDetailsEntity Details { get; set; }
public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}

关于c# - 使用 LINQ/C# 使用 JSON 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31554714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com