score:6
not sure if this the optimum way to handle it, but you could use something like this:
if ( jsonres.selecttoken("result") is jobject )
{ ... }
else if ( jsonres.selecttoken("result") is jarray)
{ ... }
else
{ ...some exception perhaps }
edit: slight improvisation
if ( jsonres.selecttoken("result") is jobject )
{ //create jarray with the lone "result" in it }
//use the jarray
score:0
you could create jsonconverter for that e.g.
public class singleorlistjsonconverter : jsonconverter
{
public override bool canwrite => false;
public override void writejson(jsonwriter writer, object value, jsonserializer serializer)
{
throw new notimplementedexception();
}
public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)
{
var token = jtoken.readfrom(reader);
switch (token.type)
{
case jtokentype.object:
var item = token.toobject<myresulttype>();
return new list<myresulttype>(new[] {item});
case jtokentype.array:
return token.toobject<list<myresulttype>>();
default:
return new list<myresulttype>();
}
}
public override bool canconvert(type objecttype)
{
return objecttype == typeof(list<myresulttype>) || objecttype == typeof(myresulttype);
}
}
and then just decorate your property with this converter
public class myclass
{
[typeconverter(typeof(singleorlistjsonconverter))]
public myresulttype result { get; set; }
}
i guess we could even make this converter generic, but this is just a simple example.
score:1
this is quite an old question, but i've got an answer for the system.text.json namespace in case you do not want to use the newtonsoft.json namesapce. there is also a parse method within jsondocument where you can determine the class of the root element.
var jdocument = system.text.json.jsondocument.parse(json);
if (jdocument.rootelement.valuekind == jsonvaluekind.object)
{
//object
}
if (jdocument.rootelement.valuekind == jsonvaluekind.array)
{
//array or list
}
score:3
this will work:
jobject jsonres = jobject.parse(json);
console.writeline(jsonres["result"].type);
to find out whether it's an object or array. you can use a switch-case on the enum:
switch(jsonres["result"].type)
{
case jtokentype.object:
//do something if it's an object
break;
case jtokentype.array:
//do something if it's an array
break;
}
Source: stackoverflow.com
Related Query
- How to tell if JSON result has object or array?
- How to convert a list of anonymous object to string array using linq for return using Json Result in ASP.NET MVC
- How to tell if a Linq object has been attached to a data context?
- How query specific item from json object which has dictionary?
- How can I get LINQ to return the object which has the max value for a given property?
- How to convert a 2-d array into a dictionary object
- How can I get LINQ to return the index of the object which has the max value in a collection?
- Create JSON object instead of an Array using LINQ/JavaScriptSerializer
- LINQ to JSON - Query for object or an array
- How to I read a json file containing array with C# and perform LINQ query on it?
- How to get data in string array or JSON format using linq to entities?
- How can I generate all possible LINQ strings of a json object for Json.net?
- How to easly convert linq result to Business Object Collection <T>
- How can I use Linq to build a c# object from xml where an element has zero or more elements of the same type?
- How to insert the LINQ result to a string[] array
- How to get a single object by LINQ? ERROR: The result of a query cannot be enumerated more than once
- How to determine if a JSON object contains only a specific key?
- How to search on Cosmos DB in a complex JSON Object
- LINQ to JSON - Select array object element where specific property matches
- C# linq to json how to read a property value within a Array
- How to return this format this JSON into an array of strings
- How to find child element from Json or class object using Linq
- How to determine if a newly created object has had any property values set by user
- how to bind one dimensional string array result to datagrid column header
- C# How to declare new Dictionary object based off a string array within a LINQ query
- Linq - How to take the result for a query in Linq and add it to an Array
- How to check list has all array items?
- How I could loop over an array of objects and add a property from each object to a variable using Linq?
- How to neatly query corresponding object array items?
- Using LINQ, where one object property has a value of x, how do I return the value in a different property
More Query from same tag
- How to do multiple left outer joins in a Linq Query?
- LINQ Query Syntax to Lambda
- KeyValuePair deconstruction in ToDictionary
- Why is Entity Framework generating a JOIN on SELECT
- SQL search between commas
- Get all Image Elements within a Grid->Canvas using LINQ?
- The type arguments for method Queryable.Select cannot be inferred from the usage
- how to count and group by using linq
- How do I get a list of player without making its own class?
- How to Compare strings in Linq Query
- Multiple Table Join in Linq C# Dynamically
- display xml using linq
- What are the advantages/disadvantages of assigning a name to one or more of the properties a LINQ anonymous type?
- Convert this LINQ code back to a Loop (or Why is this object sometimes null)
- CASE or IF Statement in Linq Query?
- Linq woes - A CollectionType is required
- DataGridView is not able to populate by LINQ to SQL object
- Is there a way to use Linq to display an item with the highest result without duplication of a specific name?
- Include nested entities using LINQ
- Linq to update a collection with values from another collection?
- How to parse a XML file using Linq - descendants
- Linq Filter IEnumerable against another IEnumerable
- How to return all values of a column as null except one column value in LINQ?
- How to avoid reading same records from SQL Database from multiple threads in LINQ to Entities
- Joining two lists in LINQ
- Filtering data according to date range mvc4
- Associate values between two lists with LINQ?
- How to substitute the ThreadPool.QueueUserWorkItem() by Task.Factory.StartNew()?
- How to detect when there is no value from database SQL LINQ C#
- LinQ method syntax multiple tables with .where() and without joins