使用LINQ / C#

时间:2015-07-22 05:41:05

标签: c# json linq

我正在尝试处理大量文件并将其数据移动到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中?

我收到的错误是:

  

无法反序列化当前的JSON对象(例如{" name":" value"})   进入类型' System.Collections.Generic.List`1

     

要修复此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为正常的.NET   type(例如,不是像整数这样的基本类型,不是集合类型   像数组或List一样,可以从JSON对象反序列化。   JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。

我不太明白它向我表明了什么,而且我确定我的班级结构是问题,但我需要一些指导如何解决它。

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

主要实体     公共类ResourceOrdersJSONEntity     {         公开列表详细信息{get;组; }         公共清单订单{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; }
}

3 个答案:

答案 0 :(得分:3)

我只需将ResourceOrdersJSONEntity更改为:

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

答案 1 :(得分:2)

尝试使用JsonConvert.DeserializeObject<ResourceOrdersJSONEntity>(sampleJson)方法。它将JSON强制转换为指定的类。

如下所述。您必须将主要实体更改为此

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}

在JSON对象中用大括号{}括起来,数组(JSON中唯一一种列表/数组/集合类型)用方括号[]括起来。需要将JSON对象转换为C#对象,并且需要将JSON数组转换为C#list / array / collection。这就是为什么当您尝试将"details"对象转换为List时,JSON.net会给您一个错误

答案 2 :(得分:0)

您的ResourceOrdersJSONEntity类应如下所示:

public class ResourceOrdersJSONEntity 
{ 
   public List Details { get; set; } 
   public List<ResourceOrdersQuantityEntity> Orders { get; set; } 
}