There is no formal way to assert if json string is a single object of array.
If you were using Json.NET yes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace IsJsonType
{
class Program
{
static void Main(string[] args)
{
Test1();
Test2();
Console.ReadLine();
}
private static void Test1()
{
var json = @"[{""Name"": ""11"",""RootPath"": ""\\\\somepath\\HTTP\\abc""},{""Name"": ""22"",""RootPath"": ""\\\\somepath\\HTTP\\def""}]";
var token = JToken.Parse(json);
if (token is JArray)
{
Console.WriteLine("array");
}
else if (token is JObject)
{
Console.WriteLine("single object");
}
}
private static void Test2()
{
var json = @"{""Name"": ""aaa"",""RootPath"": ""\\\\dddd\\HTTP\\qqqq""}";
var token = JToken.Parse(json);
if (token is JArray)
{
Console.WriteLine("array");
}
else if (token is JObject)
{
Console.WriteLine("single object");
}
}
}
}
Or this library yes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LightWeightJsonParser;
namespace IsJsonType
{
class Program
{
static void Main(string[] args)
{
Test1();
Test2();
Console.ReadLine();
}
private static void Test1()
{
var json = @"[{""Name"": ""11"",""RootPath"": ""\\\\somepath\\HTTP\\abc""},{""Name"": ""22"",""RootPath"": ""\\\\somepath\\HTTP\\def"" }]";
var jsonObject = LWJson.Parse(json);
Console.WriteLine(jsonObject.IsArray);
}
private static void Test2()
{
var json = @"{""Name"": ""aaa"",""RootPath"": ""\\\\dddd\\HTTP\\qqqq""}";
var jsonObject = LWJson.Parse(json);
Console.WriteLine(jsonObject.IsArray);
}
}
}