I am trying to deserialize a GraphQLResponse in c#.
My response contains the following:
{
"myClassObjects": [
{
"var1": "var1Value",
"var2": "var2Value"
}, //more MyClass objects
]}
According to several sources, I should now be able to unpack and deserialize this using:
var output = response.getDataFieldAs<IEnumerable<MyClass>>("myClassObjects");
Sadly, the getDataFieldAs method does not exist (anymore).
The response.Data is a Newtonsoft.Json.Linq.JObject which contains an object "myClassObjects" which is an array of MyClass objects. When I try to unpack :
List<MyClass> output = JsonConvert.DeserializeObject<List<MyClass>>(response.Data.ToString());
I keep getting :
Cannot deserialize the current JSON object ... because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly
It's my first time working with Json and I really hope there is a handy sollution for this ? Any help would be much appreciated.
you're missing the rootObject when deserializing the "array" of, what i am assuming, is your MyClass objects.
public class RootObject {
[JsonProperty("myClassObjects")]
public List<MyClass> MyClassObjects {get;set; }
}
// then deserialize your json like this,
var obj = JsonConvert.DeserializeObject<RootObject>(response.Content);
Without the details on how you are getting the response, i am not sure how to explain the difference in response.Content vs response.Data. If you are using RestSharp, then Data would have the correctly deserialized data for you. Content is the raw string that you would deserialize yourself.
The error you keep getting is because you are deserializing the Json as [ ] but its { property: value }. Your json has an object that has an array instead of json being an array of objects.
Related
I have the following problem:
I am a C# beginner and I want to create a weather app using the OpenWeatherApi.
The response of the api looks like this (it´s just an example):
[
{
"name": "London",
"lat": 51.5085,
"lon": -0.1257,
"country": "GB"
},
I want to get the lat and lon values.
My C# code looks like this:
public class CoordinatesResponse
{
public double Lat { get; set; }
public double Lon { get; set; }
}
private static string _coordinatesResponse;
static void ExtractCoordinatesFromJson()
{
CoordinatesResponse coordinatesresponse = JsonConvert.DeserializeObject<CoordinatesResponse>(_coordinatesResponse);
Console.WriteLine(coordinatesresponse.Lat);
Console.WriteLine(coordinatesresponse.Lon);
}
My error is the following:
Unhandled exception. Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WeatherApp.CoordinatesResponse' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
What do I wrong?
Your problem is in the type. Been an array, you must use List<CoordinatesResponse>:
static void ExtractCoordinatesFromJson()
{
var coordinatesresponses = JsonConvert.DeserializeObject<List<CoordinatesResponse>>(_coordinatesResponse);
foreach (var coordinatesresponse in coordinatesresponses)
{
Console.WriteLine(coordinatesresponse.Lat);
Console.WriteLine(coordinatesresponse.Lon);
}
}
To solve the issue " Cannot deserialize the current JSON array (e.g. [1,2,3])"
Follow the steps:
Go to any website that convert JSON to C# (e.g. https://json2csharp.com/).
Paste your JSON data in the JSON section and click on generate C# link button.
It will generate the object to deserialize the json array.
e.g. JsonDataService myData = JsonConvert.DeserializeObject(json);
The above object can be used to manipulate the JSON string object.
I have a Post method that receives a fairly simple JSON:
{
"First": "John",
"Last": "Smith"
}
But I need it as a string.
I've tried casting the JSON to a string but it comes up null.
public void Post([FromBody]string jsonData)
{
...
}
I would like to use jsonData as a string. I am trying to avoid creating a class with field names matching the JSON because that would mean changing the API every time a field is added. I just need to receive the JSON as a string at the outset. Is there a way to cast it as such at the endpoint without using variable names?
Just use JToken as parameter.
[HttpPost]
public void PostMethod([FromBody] JToken json)
{
var jsonAsString = json.ToString();
}
Have you try this?
JObject json = JObject.Parse(str);
You might want to refer to Json.NET documentation
I'm using JSON.net in C# for an Excel VSTO Add in and pulling in JSON via web service.
I have verified the JSON I pull is valid (online JSON Validator) but am unable to convert the JSON into Objects in C# to use.
When I run the code below I get the exception below.
Any ideas on who I can covert the JSON correctly?
Exception:
Newtonsoft.Json.JsonSerializationException:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Bliss.Ribbon1+RootObject[]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
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<T>)
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.
Code:
public async Task<string> getline()
{
<--- Set Client, Execute Request --->
//JSON content shown below
string content = await response.Content.ReadAsStringAsync();
RootObject[] dims = JsonConvert.DeserializeObject<RootObject[]>(content);
return content;
}
public class RootObject
{
public List<string> ledger { get; set; }
public List<string> ledgerAlias { get; set; }
public List<string> entity { get; set; }
public List<string> entityAlias { get; set; }
public List<string> scenario { get; set; }
public List<string> period { get; set; }
public List<string> location { get; set; }
public List<string> measures { get; set; }
}
JSON:
{
"acc":["A","B"],
"accAlias":["ACE","BCE"],
"company":["PEP", "KO"],
"companyAlias":["Pepsi", "Coco-Cola"],
"scenario":["Sales", "Expenses"],
"year": ["2016","2017"],
"areaCode":["123","131","412"],
"clients":["32340-0120","3031-0211","3412-0142"]
}
The JSON represents a single instance of the object, not an array. So instead of this:
RootObject[] dims = JsonConvert.DeserializeObject<RootObject[]>(content)
use this:
RootObject dims = JsonConvert.DeserializeObject<RootObject>(content)
Conversely, if it should be an array, make the JSON itself an array (containing a single element) by surrounding it with brackets:
[{
"acc":["A","B"],
"accAlias":["ACE","BCE"],
"company":["PEP", "KO"],
"companyAlias":["Pepsi", "Coco-Cola"],
"scenario":["Sales", "Expenses"],
"year": ["2016","2017"],
"areaCode":["123","131","412"],
"clients":["32340-0120","3031-0211","3412-0142"]
}]
Edit: As others have also been pointing out, the properties on the JSON object don't actually match that class definition. So while it may "successfully" deserialize, in doing so it's going to ignore the JSON properties it doesn't care about and initialize to default values the class properties.
Perhaps you meant to use a different class? Or different JSON? Or rename one or more properties in one or the other?
Either way, the difference between a single instance and an array of instances is the immediate problem. But in correcting that problem you're going to move on to this next one.
The RootObject and the json are not compatible. You could deserialize using a dictionary. Try this:
var dims = JsonConvert.DeserializeObject<Dictionary<string, string[]>>(content);
I have a sample Json
[{"_2":["HR Data","Reformed (Master File)"]}]
and I am trying to deserialize it into below model
public class ExploreCriteria
{
public Dictionary<String, List<String>> Explore { get; set; }
}
this is what I have tried so far
ExploreCriteria Explore = new ExploreCriteria();
Explore = JsonConvert.DeserializeObject<ExploreCriteria>(JsonStr);
but it says
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'DataModels.ExploreCriteria' because the type requires a JSON object
(e.g. {"name":"value"}) to deserialize correctly.
The provided JSON and your ExploreCriteria class do not describe the same structure.
Your JSON structure is an array that contains a key with an array value. So you can either remove the square brackets to
{"_2":["HR Data","Reformed (Master File)"]}
then your ExploreCriteria is fitting. Or you can change the JsonConvert call to
var JsonStr = "[{\"_2\":[\"HR Data\",\"Reformed(Master File)\"]}]";
ExploreCriteria Explore = new ExploreCriteria();
var data = JsonConvert.DeserializeObject<IEnumerable<Dictionary<String, List<string>>>>(JsonStr);
Explore.Explore = data.FirstOrDefault();
List<KeyValuePair<string, List<string>>> uploadedfiles =
JsonConvert.DeserializeObject<List<KeyValuePair<string, List<string>>>>(json);
use keyvaluepair class instead of dictionary.
I have data in the following format. I want to convert this data to objects.
Result = {
"Location": [
"bangalore",
1,
"chennai",
1,
"mumbai",
1,
"delhi",
0,
"Agra",
0
]
}
In my Location.cs i have the following fields. i want to assign the data to this fields. How can i achieve this
public string loc { get; set; }
public int count { get; set; }
I tried with
Location = Result.ToObject<List<Location>>();
but not working getting the following error
{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[Location]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo 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.\r\nPath 'Location'."}
Take a look at the native json deserializaton that is part of .NET
MSDN - How to: Serialize and Deserialize JSON Data
The problem is this: Result is a JSON object, not a JSON array, therefore you can't convert it to a List<Location>.
You would need a class that contains a list of locations and convert to that class:
public class LocationsContainer
{
public List<Location> Location { get; set; }
}
Result.ToObject<LocationsContainer>();
Try with Json.NET library.
List<Location> locations = JsonConvert.DeserializeObject<List<Location>>(result);