4

I'm trying to process a number of files and move their data into a SharePoint List. I'm completely comfortable with the SharePoint component of my request but I'm struggling to wrap my head around taking JSON and performing C# statements against it.

The JSON I'm working with is in the following format:

{
  "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
    }
  ]
}

Based upon this StackExchange request I've tried creating a class to deserialize my content into using the following:

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

This isn't working for me and I suspect I'm looking down the completely wrong path. What do I need to be learning about in order to take the JSON and wrap linq statements around it for processing into SharePoint?

The error I'm receiving is:

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.

I don't quite understand what it's indicating to me and I'm sure it's my class structure that's the problem, but I need a bit of guidance on how to fix it.

Here are the classes I've created for deserialization:

Main Entity public class ResourceOrdersJSONEntity { public List Details { get; set; } public List Orders { get; set; } }

Details

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; }
}

Address Entity

   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; }
    }

Questions Entity

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; }
}

Orders Entity

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; }
}
Community
  • 1
  • 1
Michael A
  • 9,480
  • 22
  • 70
  • 114
  • 1
    Have you tried this var contactPerson = JsonConvert.DeserializeObject>(sampleJSON); – Light Jul 22 '15 at 05:46
  • Looks like you are on the right path. Can you paste in the code for class `ResourceOrdersJSONEntity`? Also, when you say its not working, what is the problem exactly? – Mike Hixson Jul 22 '15 at 05:51
  • @Light I've just updated the code with that and still having issues. I suspect my classes are the problem (now added to question)? – Michael A Jul 22 '15 at 05:55
  • @MikeHixson Added to question! Sorry was sitting in notepad and I forgot to bring it over after removing company data – Michael A Jul 22 '15 at 05:57

3 Answers3

3

I get your code to work simply by changing ResourceOrdersJSONEntity to:

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}
Mike Hixson
  • 5,071
  • 1
  • 19
  • 24
2

Try using the JsonConvert.DeserializeObject<ResourceOrdersJSONEntity>(sampleJson) method. It casts the JSON to the specified class.

As mentioned below. You'll have to change the Main entity to this

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

In JSON objects are enclosed in curly brackets {} and arrays (which is the only kind of list/array/collection type in JSON) are enclosed in square brackets []. JSON objects need to be converted to C# objects and JSON arrays need to be converted to a C# list/array/collection. This is why JSON.net is giving you an error when you are trying to convert the "details" object to a List

Jon G Stødle
  • 3,844
  • 1
  • 16
  • 22
  • I've since made this change but still receiving an error - added classes and exception to the question since it looks to be like I'm on the right track but making errors somewhere in the construction of my classes – Michael A Jul 22 '15 at 06:00
0

Your ResourceOrdersJSONEntity class should be like this:

public class ResourceOrdersJSONEntity 
{ 
   public List Details { get; set; } 
   public List<ResourceOrdersQuantityEntity> Orders { get; set; } 
}
Light
  • 1,077
  • 10
  • 33