IDataUnpacker.ContainsKey(string key) Method
.NET Standard 2.x
Checks whether the specified key exists in the IDataUnpacker
object.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
bool ContainsKey(string key);
Parameters
key
System.String
The key of the element to look for.
Returns
System.Boolean
Returns true
if the element exists; otherwise, false
.
Examples
The following code example checks if the keys exists in an IDataUnPacker
object.
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.IDataUnpackerExamples
{
public class ContainsKeyExample
{
private readonly SchoolContext _context;
public ContainsKeyExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
// Normally, you should receive a JSON string in the Response Body.
// It creates a DataPacker object here just for this example.
var packer = new DataPacker();
// Adds an element with the key "Key1" and its value "Value1".
packer.AddValue("Key1", "Value1");
var json = packer.GetTextString(DataFormat.Json);
Console.WriteLine("JSON String: {0}", json);
// Creates a DataUnpacker object using the JSON string.
// In a controller class, you can also use a IDataUnpacker type as a
// parameter to receive a JSON Package format JSON string from the
// Request Body.
var dataUnPacker = new DataUnpacker(json, DataFormat.Json);
// Checks if IDataUnPacker contains "Key1" and returns true.
Console.WriteLine("Contains Key1: {0}",
dataUnPacker.ContainsKey("Key1"));
// Checks if IDataUnPacker contains "Key2" and returns false.
Console.WriteLine("Contains Key2: {0}",
dataUnPacker.ContainsKey("Key2"));
/* This code example produces the following output:
JSON String: {"Key1":"Value1"}
Contains Key1: True
Contains Key2: False
*/
}
}
}
Applies to
.NET Standard
2.x