JSON Deserialization Error in C# - c#

I want to Deserialize a JSON object to C# but I'm getting this exception:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'FYP___Task_1.RootObject' 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.
Path '', line 1, position 1.
I've tried to get this error solved by different solutions I found on StackOverflow but no one worked.
The JSON I'm using is as follows:
[
{
"rating_count": 158271,
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"language": [
"English",
"French",
"Mandarin"
],
"rating": 6.7,
"country": [
"France",
"USA"
],
"release_date": 20021011,
"title": "Transporter\n \"The Transporter\"",
"year": 2002,
"filming_locations": "Avenue de Saissy, Cannes, Alpes-Maritimes, France",
"imdb_id": "tt0293662",
"directors": [
"Corey Yuen"
],
"writers": [
"Luc Besson",
"Robert Mark Kamen"
],
"actors": [
"Jason Statham",
"Qi Shu",
"Matt Schulze",
"François Berléand",
"Ric Young",
"Doug Rand",
"Didier Saint Melin",
"Tonio Descanvelle",
"Laurent Desponds",
"Matthieu Albertini",
"Vincent Nemeth",
"Jean-Yves Bilien",
"Jean-Marie Paris",
"Adrian Dearnell",
"Alfred Lot"
],
"also_known_as": [
"Transporter"
],
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTk2NDc2MDAxN15BMl5BanBnXkFtZTYwNDc1NDY2._V1_SY317_CR3,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/000/293/662.jpg!cover?_upt=cd37cf0e1385015165"
},
"runtime": [
"92 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt0293662/"
}
]
The classes I'm using:
public class Poster
{
public string imdb { get; set; }
public string cover { get; set; }
}
public class RootObject
{
public int rating_count { get; set; }
public List<string> genres { get; set; }
public string rated { get; set; }
public List<string> language { get; set; }
public double rating { get; set; }
public List<string> country { get; set; }
public int release_date { get; set; }
public string title { get; set; }
public int year { get; set; }
public string filming_locations { get; set; }
public string imdb_id { get; set; }
public List<string> directors { get; set; }
public List<string> writers { get; set; }
public List<string> actors { get; set; }
public List<string> also_known_as { get; set; }
public Poster poster { get; set; }
public List<string> runtime { get; set; }
public string type { get; set; }
public string imdb_url { get; set; }
}

Your JSON object has the structure [ {..} ] which means it is a list of objects. In your case, your list has only one object, but it is still a list. What you are trying to do is turn the list into an object, so you get an exception.
The solution is either to change your JSON to {..} (i.e. remove the square brackets) OR deserialize the JSON into an array of RootObject and then just read the first one, for example:
RootObject[] myArray = json.Deserialize<RootObject[]>("json goes here");
RootObject firstObject = myArray[0];

We can try the below option also.
var root = response1.Content.ReadAsAsync<RootObject>().Result;
GridView1.DataSource = root.items;

Related

JSON deserialize same object within object [duplicate]

This question already has answers here:
Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly
(6 answers)
Closed 8 months ago.
I have a JSON file which looks like this
{
"Response": {
"Part": {
"Name": "Part1",
"Rev": "01",
"Title": "Part1",
"current": "Released",
"Part": [
{
"Name": "Part2",
"Rev": "00",
"Title": "Part2",
"current": "Released",
"Part": {
"Name": "Part3",
"Rev": "R00",
"Title": "Part3",
"current": "Released"
}
},
{
"Name": "Part4",
"Rev": "00",
"Title": "Part4",
"current": "Released"
}
]
}
}
}
I have created my class objects as this
public class PartObj
{
public string Name { get; set; }
public string Rev { get; set; }
public string Title { get; set; }
public string current { get; set; }
public List<PartObj> Part { get; set; }
}
public class Response
{
public PartObj Part { get; set; }
}
public class Root
{
public Response Response { get; set; }
}
But I am unable to deserialize the JSON string
Root items = JsonConvert.DeserializeObject<Root>(jsonStr);
The error says
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[PartObj ]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Any Solution for Deserializing this?
'Part' : member names cannot be the same as their enclosing type
This is why its happening:
The members of a class or struct cannot have the same name as the class or struct.
Try the following approach it should work just fine.
public class Root
{
public Response Response { get; set; }
}
public class Response
{
public Part Part { get; set; }
}
public class Part
{
public string Name { get; set; }
public string Rev { get; set; }
public string Title { get; set; }
public string current { get; set; }
public List<Part> Parts { get; set; }
}

How to deserialize json inside a json to a string property

I am reading a json-string from an API (in a script component i SSIS). The json looks something like the one below (this is only one record, the real string contains many more). When trying to deserialize into a class containing the needed properties, I would like to put the "value" for the "costCenters"-property into a string property in my table. How ever, since the costCenters-value in itself contains a JSON, the deserialization fails (because it encounters the objects inside it).
If I exclude the property CostCenters from my class, it manages to deserialize the other properties just fine. I would think (hope) that it would be possible to force the inner JSON into a string property? Any suggestions?
This is my class that is used for the deserilazing:
internal class Unit
{
public int ParentId { get; set; }
public int CompanyId { get; set; }
public string Abbreviation { get; set; }
public string AbbreviationPath { get; set; }
public int Manager { get; set; }
public int UnitId { get; set; }
public string Name { get; set; }
public string LevelDescription { get; set; }
public int OrfuCompanyId { get; set; }
public string Resh { get; set; }
//If the below propery is commented out, the deserializing works fine.
public string CostCenters { get; set; }
public int Level { get; set; }
public int OrfuUnitId { get; set; }
}
This is how I call the deserializer for NewtonSoft:
var units = new List<Unit>();
units = JsonConvert.DeserializeObject<List<Unit>>(jsonString);
This is how the jsonString looks (edited):
[
{
"$id": "1",
"parentId": 999,
"companyId": 9123,
"abbreviation": "ZZZ",
"abbreviationPath": "SOMEPATH",
"costCenters": [
{
"$id": "12",
"costCenter": "12345",
"costCenterSourceId": "99",
"costCenterSource": "TBF",
"costCenterTypeId": "999",
"costCenterType": "TypeOfCostCenter",
"startDate": "2018-01-01T00:00:00",
"endDate": "9999-12-31T00:00:00"
},
{
"$id": "88",
"costCenter": "191945444",
"costCenterSourceId": "88",
"costCenterSource": "TBB",
"costCenterTypeId": "15",
"costCenterType": "SomeTextHere",
"startDate": null,
"endDate": null
}
],
"manager": 12345678,
"deputy": 0,
"homeShare": "\\\\someaddress.net\\someFolder\\SomeCode",
"objectGuid": "ThisIsAGUID",
"distinguishedName": "OU=ABC,OU=NNN,OU=FFF,OU=HHH,OU=HNV,OU=IDK,DC=heipaadeg,DC=com",
"orfuUnitId": 9125,
"orfuCompanyId": 9123,
"resh": "123456789",
"nis": "",
"unitId": 4321,
"name": "2 some name",
"level": 9,
"levelDescription": "Level number 4"
}
]
One workaround is to add a field to Unit called CostCentersString in which the CostCenters list is re-serialized:
In Unit class definition:
...
public List<dynamic> CostCenters { get; set; }
public String CostCentersString { get; set; }
Then use:
List<Unit> units = JsonConvert.DeserializeObject<List<Unit>>(jsonString);
foreach (Unit u in units)
{
u.CostCentersString = JsonConvert.SerializeObject(u.CostCenters);
}

Use Json file as db

I am starting c# and I was asked to create a small Api using a given Json file as DB.
My json file looks like:
{"Items": [
{
"id": 1,
"name": "Apple",
"price": 12.50,
"image": "some url",
"description": "Some text",
"tags": [ "fruit", "red" ],
"category": "fruit"
},
]}
I created a model:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Image { get; set; }
public string Description { get; set; }
public List<Tags> Tags { get; set; }
public string Category { get; set; }
}
public class Tags
{
public string Tag { get; set; }
}
And my controller looks like:
[ApiController]
[Route("api/[controller]")]
public class ProductController : Controller
{
[HttpGet]
public IEnumerable<Product> Get()
{
StreamReader r = new StreamReader("./items.json");
string jsonString = r.ReadToEnd();
List<Product> productsList = JsonConvert.DeserializeObject<List<Product>>(jsonString);
return productsList;
}
}
The error I get:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Kata.Product]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
As I am new to c#, I am having trouble to find out how to simply return what I have in my Json file. At some point I was able to return the list by changing the json file(not the greatest idea) but all the items were null.
Thank you
You have a bug in your json classes. You can't use a special class for the tags, try to replace it by string array or list. The same about items. You deserialized the whole Root object, items just a property of this object. So since you don't need the whole object but just list of items, you have to select only them.
var jsonDeserialized= JsonConvert.DeserializeObject<Root> (jsonString);
List<Product> productsList = jsonDeserialized.Items ;
classes
public class Product
{
public int id { get; set; }
public string name { get; set; }
public double price { get; set; }
public string image { get; set; }
public string description { get; set; }
public List<string> tags { get; set; }
public string category { get; set; }
}
public class Root
{
public List<Product> Items { get; set; }
}
and fix json
{
"Items": [{
"id": 1,
"name": "Apple",
"price": 12.50,
"image": "some url",
"description": "Some text",
"tags": ["fruit", "red"],
"category": "fruit"
}]
}

Deserialize JSON not working with newtonsoft.json

I have a json String as below, which i am using the deserialize into my class objects but I can't, here is my json.:
{
"TraceId": "24bf6a01-5d8f-4959-9173-20600a04b738",
"TransactionId": "AFC48AE50A076477C3E069296AC3F884",
"ResponseTime": "1672",
"DistanceUnits": "MI",
"CurrencyType": "GBP",
"xmlns:air": "http://www.travelport.com/schema/air_v42_0",
"air:FlightDetailsList": {
"air:FlightDetails": [
{
"Key": "hx5kk+3R2BKAuFzqAAAAAA==",
"Origin": "DXB",
"Destination": "LHE",
"DepartureTime": "2017-12-10T13:55:00.000+04:00",
"ArrivalTime": "2017-12-10T17:55:00.000+05:00",
"FlightTime": "180",
"TravelTime": "180",
"Equipment": "320",
"OriginTerminal": "1",
"DestinationTerminal": "M"
},
{
"Key": "hx5kk+3R2BKAwFzqAAAAAA==",
"Origin": "LHE",
"Destination": "DEL",
"DepartureTime": "2017-12-20T12:15:00.000+05:00",
"ArrivalTime": "2017-12-20T14:10:00.000+05:30",
"FlightTime": "85",
"TravelTime": "690",
"Equipment": "ATR",
"OriginTerminal": "M",
"DestinationTerminal": "3"
},
{
"Key": "hx5kk+3R2BKAyFzqAAAAAA==",
"Origin": "DEL",
"Destination": "DXB",
"DepartureTime": "2017-12-20T20:25:00.000+05:30",
"ArrivalTime": "2017-12-20T22:45:00.000+04:00",
"FlightTime": "230",
"TravelTime": "690",
"Equipment": "788",
"OriginTerminal": "3",
"DestinationTerminal": "1"
}
]
}
}
So I want to convert into my class LowFareSearchRsp and its FlightDetails class is also shown below:
public class LowFareSearchRsp
{
public string TraceId { get; set; }
public string TransactionId { get; set; }
public string ResponseTime { get; set; }
public string DistanceUnits { get; set; }
public string CurrencyType { get; set; }
public string air { get; set; }
public FlightDetailsList FlightDetailsList { get; set; }
}
public class FlightDetailsList
{
public List<FlightDetails> FlightDetails { get; set; }
}
public class FlightDetails
{
public string Key { get; set; }
public string Origin { get; set; }
public string Destination { get; set; }
public DateTime Departure { get; set; }
public DateTime ArrivalTime { get; set; }
public string FlightTime { get; set; }
public string TravelTime { get; set; }
public string Equipment { get; set; }
public string OriginTerminal { get; set; }
public string DestinationTerminal { get; set; }
}
and I'm using the NewtonSoft library to deserialize that json to my class object but getting the following error:
{"Cannot deserialize the current JSON object (e.g.
{\"name\":\"value\"}) into type
'System.Collections.Generic.List`1[ParseSoapEnveloperReqRes.LowFareSearchRsp]'
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 'TraceId', line 2, position
12."}
Here is my code to deserialize the json string:
var LowFareSearchRsps = JsonConvert.DeserializeObject<List<LowFareSearchRsp>>(jsonString);
I'he had searched the whole internet, but can't find the solution of this. Kindly Help. Thanks.
Your JSON string represent an object not an array change deserilize code to be as below:
var LowFareSearchRsps = JsonConvert.DeserializeObject<LowFareSearchRsp>(jsonString);
Your JSON does not represent a List<LowFareSearchRsp> but a LowFareSearchRsp.

Deserializing JSON Object into C# list

I have some JSON that I want to deserilize into a list<> object.
I am using JsonConvert by Newtonsoft and I have setup my public classes to support the list. These are as follows.
public class NewDocumentObject
{
public int ContractId { get; set; }
public int FolderId { get; set; }
public string CreatedAt { get; set; }
public string CreatedBy { get; set; }
public string TemplateReference { get; set; }
public bool IsTest { get; set; }
public bool IsExplicitName { get; set; }
public object Requester { get; set; }
public object ExternalReference { get; set; }
public object ExternalLabel { get; set; }
public string Status { get; set; }
public string StatusLabel { get; set; }
public int Share { get; set; }
public int EffectiveRight { get; set; }
public string Name { get; set; }
public object ModifiedAt { get; set; }
public object ModifiedBy { get; set; }
public object ModifiedById { get; set; }
public object ProfileReference { get; set; }
public object ESignatureId { get; set; }
public List<object> Documents { get; set; }
public object Folder { get; set; }
public object Session { get; set; }
public string ESignatureStatus { get; set; }
public List<object> Alerts { get; set; }
public List<Link> Links { get; set; }
}
public class Link
{
public string rel { get; set; }
public string method { get; set; }
public string href { get; set; }
}
The JSON is as follows.
{
"ContractId": 103,
"FolderId": 6,
"CreatedAt": "2016-02-18T11:30:17.293",
"CreatedBy": "SMTC",
"TemplateReference": "Non Disclosure Agreement",
"IsTest": false,
"IsExplicitName": false,
"Requester": null,
"ExternalReference": null,
"ExternalLabel": null,
"Status": "Incomplete",
"StatusLabel": "Incomplete",
"Share": 0,
"EffectiveRight": 3,
"Name": "Non Disclosure Agreement",
"ModifiedAt": null,
"ModifiedBy": null,
"ModifiedById": null,
"ProfileReference": null,
"ESignatureId": null,
"Documents": [],
"Folder": null,
"Session": null,
"ESignatureStatus": "",
"Alerts": [],
"Links": [{
"rel": "questionnaire",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/questionnaire/pages/1?navigate=first"
}, {
"rel": "answers",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/answers"
}, {
"rel": "documents",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103/documents"
}, {
"rel": "template",
"method": "get",
"href": "http://srv-dev-29/api/templates/Non Disclosure Agreement"
}, {
"rel": "folder",
"method": "get",
"href": "http://srv-dev-29/api/folders/6"
}, {
"rel": "self",
"method": "get",
"href": "http://srv-dev-29/api/contracts/103"
}]}
To deserialize the JSON I have the following line.
List<NewDocumentObject> newDoc = JsonConvert.DeserializeObject<List<NewDocumentObject>>(response.Content.ReadAsStringAsync().Result);
This is where it's falling over. The JsonConvertor is throwing and exception.
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ContractExpressAPITest.Form1+NewDocumentObject]' 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) 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.
Path 'ContractId', line 1, position 14.
I suspect this is because it's not able to handle the List items in the JSON.
Anyone able to help??
Thanks
Copy your JSON to the clipboard and then if your are using Visual Studio in the "Edit> Paste Special> Paste JSON as classes." and then do the same
You are trying to deserialize into a List
List<NewDocumentObject> newDoc = JsonConvert.DeserializeObject<List<NewDocumentObject>>(response.Content.ReadAsStringAsync().Result);
But your JSON string contains an object {} only.
Change your JSON input to [{...}]
Or change your deserialize call to only one object.
You cannot serialize or deserialize a List of <object>. You must use a strongly typed class.

Categories

Resources