I am reading 2 REST APIs using Httpclient in C#. The APIs return following employee data in JSON format:
1st API
{
"status": "OK",
"content": {
"empid1": 89900,
"empid2": 45550,
"empid3": 22350}
}
2nd API
{
"status": "OK",
"content": {
"empid1": "grade1",
"empid1": "grade2",
"empid1": "grade2"}}
Classes defined and code used is as follows:
public class content
{
public string empid { get; set; } // e.g. empid3
public double salary { get; set; } // e.g. 89900
public string grade { get; set; } // e.g. Grade1
}
public sealed class WrapperEmployees
{
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("data")]
public List<content> empdata { get; set; } = new List<data>();
}
To deserialize, used this-
WrapperEmployees nj = JsonConvert.DeserializeObject<WrapperEmployees>(response);
But, last line gives error on deserialization:
Cannot deserialize current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CsharpSample.App_Code.Employee]' 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 'content.emp1', line 4, position 18.
Is my class structure incorrect?
My ultimate aim is to fetch common data from both APIs against employees.
Option 1: use specific classes for each json deserialization:
class EmployeesSalaries {
public string Status { get; set; }
public Dictionary<string, int> content { get; set; };
}
class EmployeesGrades {
public string Status { get; set; }
public Dictionary<string, string> content { get; set; };
}
Option 2: deserialize to common class, but you will get 'good' content values only if they are int/string. If they will be objects - you will have JObjects as values.
class EmployeesData {
public string Status { get; set; }
public Dictionary<string, object> content { get; set; };
}
Related
I am trying to deserialize a JSON string and for some reason it will not work.
string json_string = "{'CartID':{'ID':253419,'AuthenticatedKey':223239},'CustomerID':null,'PurchaseItems':null,'TenderInformation':null,'ModifyCartItems':null,'AdditionalCartInformation':{'ServiceInformation':{'ServiceInformationItem':[{'ServiceID':243401}]}}}";
modifyCartReq = JsonConvert.DeserializeObject<ModifyCartReq>(json_string);
I have also tried the following
string json_string = "{'CartID':{'ID':253419,'AuthenticatedKey':223239},'CustomerID':null,'PurchaseItems':null,'TenderInformation':null,'ModifyCartItems':null,'AdditionalCartInformation':{'ServiceInformation':{'ServiceInformationItem':[{'ServiceID':243401}]}}}";
modifyCartReq = JsonSerializer.Deserialize<ModifyCartReq>(json_string);
I get the same error below
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'ExampleType' 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 'AdditionalCartInformation.ServiceInformation.ServiceInformationItem', line 1, position 213.
From my attempts at solving this It feels like some syntax issue is ultimately the cause of this.
you have to fix json_string
{
"CartID": {
"ID": 253419,
"AuthenticatedKey": 223239
},
"CustomerID": null,
"PurchaseItems": null,
"TenderInformation": null,
"ModifyCartItems": null,
"AdditionalCartInformation": {
"ServiceInformation": {
"ServiceInformationItem": [{
"ServiceID": 243401
}]
}
}
}
try this
modifyCartReq = JsonSerializer.Deserialize<Root>(json_string);
and use this classes
public class CartID
{
public int ID { get; set; }
public int AuthenticatedKey { get; set; }
}
public class ServiceInformationItem
{
public int ServiceID { get; set; }
}
public class ServiceInformation
{
public List<ServiceInformationItem> ServiceInformationItem { get; set; }
}
public class AdditionalCartInformation
{
public ServiceInformation ServiceInformation { get; set; }
}
public class Root
{
public CartID CartID { get; set; }
public object CustomerID { get; set; }
public object PurchaseItems { get; set; }
public object TenderInformation { get; set; }
public object ModifyCartItems { get; set; }
public AdditionalCartInformation AdditionalCartInformation { get; set; }
}
just replace objects with your real types.
I am trying to read and access a large JSON file from local directory using newtonsoft.json in c# but always gave me the error. I created two classes for accessing it.
This is my example JSON data:
{
"passages": [
{
"passage_text": "xxxxxxx",
"url": "xxxxx",
}
]
"answer":"xxxxxx",
"query_id":"Xxxxx"
}
here is the code I have tried:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
public class passages
{
public String url { get; set; }
public String passage_text { get; set; }
}
Here is the part I tried to read and access to JSON file:
String jsonPath = #"C:\Users\admin\Desktop\647\project\collection\sample_collection.json" ;
var serializer = new JsonSerializer();
StreamReader sr = new StreamReader(jsonPath);
JsonTextReader reader = new JsonTextReader(sr);
reader.SupportMultipleContent = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
collection c = serializer.Deserialize<collection>(reader);
Console.WriteLine(c.passages.url);
}
}
And it gave me this error:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'LuceneIndexApplication.passages' 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 '[0].passages', line 1, position 15.'
you are trying to map passages to from an array into a list.
so all that you should need to change is:
public class collection
{
public passages passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
should be
public class collection
{
public List<passages> passages { get; set; }
public String answers { get; set; }
public String query_id { get; set; }
}
note the List in the latter.
This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Create a strongly typed c# object from json object with ID as the name
(1 answer)
How can I deserialize JSON with C#?
(19 answers)
Closed 4 years ago.
I'm fairly new to json, and I'm trying to convert some json content to a C# class to store it for later use. I'm having a problem trying to create a class which is suitable for the data structure. An example of the json content is shown below.
Json Data
{
"FirstItem": {
"id": 1,
"type": "foo",
"colours": ["blue", "black", "green"],
"reviews": {
"positive": ["The best", "unbelievable", "Awesome"],
"negative": ["Sh*t", "Awful", "Dire", "Terrible", "Appalling"],
"neutral": ["OK", "Meh"]
}
},
"SecondItem": {
"id": 2,
"type": "bar",
"colours": ["red", "white", "yellow"],
"reviews": {
"positive": ["Great", "Amazing", "Fantastic", "Perfect", "Uplifting"],
"negative": ["Terrible", "Shocking", "abysmal"],
"neutral": ["OK", "Standard", "Vanilla"]
}
}
}
Edit:
It's worth noting that this is just a small sample of the json data, and I expect to be working with a much larger data set which is always in this format, however the FirstItem, SecondItem (names?) will always be different. I could potentially end up with a MillionthItem.
C# Class
I have created a class based on my limited understanding of the json data above. I think this is where the problem lies - I dont think this class is suitable based on the json above. Here's what I have.
public class Data
{
public string name {get; set;}
public int id {get; set;}
public string type {get; set;}
public string[] colours {get; set;}
public Reviews reviews {get; set;}
}
public class Reviews
{
public string[] positive {get; set;}
public string[] negative {get; set;}
public string[] neutral {get; set;}
}
Converting json to class
To try and convert this, I am using JsonConvert to deserialize the data to a List of a class which I have created. For example:
var client = new RestClient(url);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var result = JsonConvert.DeserializeObject<List<Data>>(response.Content);
Exception
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[namespace.class]' 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.
Summary
I'd appreciate some advice on understanding the json format above, and how I can successfuly convert this
Solution
dbc suggested storing this information in a dictionary, which was exactly what I was needing to do - this allows me to capture the name (FirstItem, SecondItem etc.) as the key and the rest of the class Data as the value.
var result = JsonConvert.DeserializeObject<Dictionary<string, Data>>(response.Content);
Just copy your json and do Edit->Paste Special->Paste Json As Classes in Visual Studio - it will create you the matching class structure.
public class Rootobject
{
public Firstitem FirstItem { get; set; }
public Seconditem SecondItem { get; set; }
}
public class Firstitem
{
public int id { get; set; }
public string type { get; set; }
public string[] colours { get; set; }
public Reviews reviews { get; set; }
}
public class Reviews
{
public string[] positive { get; set; }
public string[] negative { get; set; }
public string[] neutral { get; set; }
}
public class Seconditem
{
public int id { get; set; }
public string type { get; set; }
public string[] colours { get; set; }
public Reviews1 reviews { get; set; }
}
public class Reviews1
{
public string[] positive { get; set; }
public string[] negative { get; set; }
public string[] neutral { get; set; }
}
Then usage will be like:
var rootObject = JsonConvert.DeserializeObject<Rootobject>(jsonString);
I have this JSON:
{
"results" : [
{
"elevation" : 25.51896667480469,
"location" : {
"lat" : -26.90408425509977,
"lng" : -49.04650926589966
},
"resolution" : 152.7032318115234
}
],
"status" : "OK"
}
This class:
public class RootObject
{
public Elevacao[] results { get; set; }
public string status { get; set; }
}
public class Elevacao
{
public Decimal elevation { get; set; }
public Decimal resolution { get; set; }
public dados[] location { get; set; }
}
public class dados
{
public Decimal lat { get; set; }
public Decimal lng { get; set; }
}
This code:
public ActionResult Teste()
{
var url = "http://maps.googleapis.com/maps/api/elevation/json?locations=-26.904084255099768,-49.04650926589966&sensor=false&format=json";
var json = new WebClient().DownloadString(url);
RootObject m = JsonConvert.DeserializeObject<RootObject>(json);
return View();
}
And this error:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'TCC.Controllers.dados[]' 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.
Path 'results[0].location.lat', line 6, position 20.
where I went wrong?
In the JSON, location is an object, not an array. However, in your Elevacao class location is defined as an array. They need to match in order for deserialization to work correctly. That is what the error message is trying to tell you.
To fix it, change this line:
public dados[] location { get; set; }
To this:
public dados location { get; set; }
location in your JSON is a single object, not an array.
You want "results" from the JSONObject. Use json.results
Here is my code:
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), enc);
JsonSerializer serializer = new JsonSerializer();
JsonTextReader jsreader = new JsonTextReader(loResponseStream);
results = (mHealthData)serializer.Deserialize(jsreader, typeof(mHealthData)); ***
loResponseStream.Close();
public class mHealthData
{ // Class for the Mhealth Data
public class RootObject
{
public string source { get; set; }
public string name { get; set; }
public string type { get; set; }
public string unit { get; set; }
public double value { get; set; }
public string timestamp { get; set; }
}
}
This is the error I receive On the line marked with a **:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MHealthPlugin.mHealthData' 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<T> 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.
I have no clue how to fix this. I've tried putting List<mHealthData> practically everywhere in the code and it doesn't help. The other rest Calls I do work fine.
For reference, here is an example of what the JSON I'm trying to parse looks like:
[{"source":"hi","name":"G","type":"number","unit":null,"value":126,"timestamp":"1974-07-27T09:35:12Z"},{"source":"hi","name":"G","type":"number","unit":null,"value":120,"timestamp":"1974-07-27T09:35:12Z"}]
Your mHealthData class doesn't have anything in it, except a nested class. Take a step back and see how your data is supposed to be defined.
It looks like you want a RootObject class. In that case, the class should be:
public class RootObject
{
public string source { get; set; }
public string name { get; set; }
public string type { get; set; }
public string unit { get; set; }
public double value { get; set; }
public string timestamp { get; set; }
}
Then when you deserialize, you'll end up with an object of type RootObject[] - your JSON is simply an array, not an object containing an array.