Cannot deserialize the current JSON object (e.g. {"name":"value"}) - c#

This is my JSON data
{
"logInResult": [
{
"Name": "yogesh singh",
"cityName": "",
"img": "DefaultImage/D_Vp_Men.png",
"usrId": "374"
}
]
}
and this is my code
public async Task<ActionResult> Index()
{
HttpClient webClient1 = new HttpClient();
Uri uri = new Uri("http://m.vinipost.com/service/userprofile.svc/logIn?loginId=thyschauhan#gmail.com&pass=12345");
HttpResponseMessage response1;
response1 = await webClient1.GetAsync(uri);
var jsonString = await response1.Content.ReadAsStringAsync();
var _Data = JsonConvert.DeserializeObject<List<JClass>>(jsonString);
foreach (JClass Student in _Data)
{
ViewBag.Message += Student.Name + ", ";
}
dynamic obj = JsonConvert.DeserializeObject(jsonString);
ViewBag.Message += obj.data.Name;
return View();
}
and the error is
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MvcSumit1.Models.JClass]' 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 'logInResult', line 1, position 15.

You can't directly deserialize from your API response using JsonConvert.DeserializeObject.
Try this below code :
JObject jsonResponse = JObject.Parse(jsonString);
JObject objResponse = (JObject)jsonResponse["logInResult"];
Dictionary<string, JArray> _Data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, JArray>>(objResponse.ToString());
Hope this will help you.

You should create the following classes in order to map your json data to actual classes.
public class LogInResult
{
public string Name { get; set; }
public string cityName { get; set; }
public string img { get; set; }
public string usrId { get; set; }
}
public class RootObject
{
public List<LogInResult> logInResult { get; set; }
}
You can then store the RootObject for further processing:
var result = JsonConvert.DeserializeObject<RootObject>(jsonString);
By using the getter for the list, you can get the list and iterate it as usual.

Your question seems to be a duplicate of: Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class
You are trying to deserialize your JSON object into an JSON array.
Store just the content of logInResult into jsonString, that is:
[{"Name":"yogesh singh","cityName":"","img":"DefaultImage\/D_Vp_Men.png","usrId":"374"}]
This of course assumes that you got your JClass correct in the first place.

You're C# code thinks it is reading this:
[
{
"Name": "yogesh singh",
"cityName": "",
"img": "DefaultImage/D_Vp_Men.png",
"usrId": "374"
}
]
i.e. an array of objects, when in fact it is reading an object with a property logInResult, which is an array.

I have faced the same issue, just wanted to point out when there is an array or list exist in JSON like in logInResults is a list of a type, so while deserializing JSON convert is not able to understand that, so what you can do it create your model in this way.
Class giveName
{
givenName[] logInResult {get;set;} // can use list also will work fine
}
public class giveName2
{
public string Name {get;set;}
public string cityName {get;set;}
public string img {get;set;}
public string usrId {get;set;}
}
i will tell you why because see the first curly braces of your json object for that to work, you must have created a type(class) which has a property named logInResult, in the same way object of which the list is made up has to be provided a type and then the properties matching with list items
Note: giveName and giveName2 is the name you can give yourself it wont matter with class name

Convert to collection of object
using Newtonsoft.Json.Linq;
JObject obj = JObject.Parse(jsonString);
JArray arr = (JArray)obj["logInResult"];
IList<JClass> student= arr.ToObject<IList<JClass>>();
return View(student);
Then iterate over it.
IEnumerable<Student>
#foreach (var item in Model)
{
item.Name
item.CityName
item.Img
item.UsrId
}

Related

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly

Where I am missing info? I need to deserialize the following JSON string.
{
"data": [
{
"FirstName": "Test",
"LastName": "Test"
}
]
}
For this, I have defined my LoadData Action Method:
public async Task<ActionResult> LoadData()
{
string apiUrl = "URL";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var input = new { depot = "DATA", fromDate = "2020-06-06", toDate = "2020-06-06" };
var response1 = await client.PostAsJsonAsync("DATAOne", input);
if (response1.IsSuccessStatusCode)
{
var data = await response1.Content.ReadAsStringAsync();
var table = JsonConvert.DeserializeObject<List<PartOne>>(data);
}
}
return View();
}
For this, I have defined my class:
public class PartOne
{
public string FirstName{ get; set; }
public string LastName{ get; set; }
}
But when I try using the de-serializer, it gives an exception.
{"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[InfluxDB.Serie]' 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 'results', line 2, position 12."}
You have two options here:
1st Option, you are missing a wrapper object for data
public class Wrapper<T>
{
public T Data { get; set; }
}
and then use:
var table = JsonConvert.DeserializeObject<Wrapper<List<PartOne>>>(json).Data;
2nd Option, deserialize it first as JObject and access to data and then deserialize it to the List<PartOne>:
var jObj = JsonConvert.DeserializeObject(json) as JObject;
var jArr = jObj.GetValue("data");
var table = jArr.ToObject<List<PartOne>>();

C# - Cannot deserialize the current JSON array

I have this code:
string json2 = vc.Request(model.uri + "?fields=uri,transcode.status", "GET");
var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
var transcode = deserial["transcode"];
var serial = JsonConvert.SerializeObject(transcode);
var deserial2 = JsonConvert.DeserializeObject<Dictionary<string, object>>(serial);
var upstatus = deserial2["status"].ToString();
The json I get from the server is:
{
"uri": "/videos/262240241",
"transcode": {
"status": "in_progress"
}
}
When running it on VS2017, it works.
But on VS2010 I get the following error:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'System.Collections.Generic.Dictionary`2[System.String,System.Object]'
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 am using Newtonsoft.Json.
Any idea?
Your received json data is not a Dictionary<string, object>, it is a object
public class Transcode
{
public string status { get; set; }
}
public class VModel
{
public string uri { get; set; }
public Transcode transcode { get; set; }
}
You can use this object:
var deserial = JsonConvert.DeserializeObject<VModel>(json2);
instead of:
var deserial = JsonConvert.DeserializeObject<Dictionary<string, object>>(json2);
The best answer was deleted for some reason, so i'll post it:
var deserial = JsonConvert.DeserializeObject<dynamic>(json2);
string upstatus = string.Empty;
upstatus = deserial.transcode.status.ToString();
If your model is not well defined or it is dynamic, then use:
var deserial = JsonConvert.DeserializeObject<dynamic>(json2);
or you can try to use:
JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(json2);

.NET Json Serialization Exception for C#

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);

Deserializing an empty array using Json.NET

I've got a C# application that uses Json.NET v7.0.1. As a result of a REST call, I get back some JSON in the form of:
{
"messages": [ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
{"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
{"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]
}
If there is no message data to return, the JSON that I get back looks like this:
{
"messages": [ ]
}
My Message class looks like this:
public class Message
{
public string PhoneNumber { get; set; }
public string SmsText { get; set; }
public Message()
{
}
public Message(string phoneNumber, string smsText)
{
PhoneNumber = phoneNumber;
SmsText = smsText;
}
}
When I try to deserialize the JSON, I'm using the following code:
Message[] messages = JsonConvert.DeserializeObject<Message[]>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});
The problem I'm running into is deserializing the JSON when the messages array is empty. When that occurs, I get the following exception:
Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"})
into type 'WindowsFormTestApplication.Message[]' 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.
Path 'messages', line 2, position 15.
When the JSON array is empty, I'd like my Message[] variable to be null. Do I need a custom converter to get this deserialized or am I missing something obvious? Thanks!
You should either send just an array
[ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
{"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
{"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]
Or use some wrapper object like this
public WrapperObject
{
public Message[] messages { get; set; }
}
And then deserialize using it
var wrapper = JsonConvert.DeserializeObject<WrapperObject>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});
Create a wrapper class and deserialize the Json into it:
public class MessageArray
{
public Message[] Messages;
}
var messages = JsonConvert.DeserializeObject<MessageArray>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});

JSON object deserialize to c# object - OpenTSDB

I am dealing with JSON for the first time and getting data from OpenTSDB. I've created a c# class to deserialize the JSON to but I am getting the error 'Cannot deserialize the current JSON array' as described below.
My c# code to get JSON:
var request = WebRequest.Create("http://localhost:4242/api/query?start=2013/08/21-12:00:00&end=2013/08/22-12:00:00&m=sum:tcollector.collector.lines_sent&o=&yrange=%5B0:%5D&wxh=924x773");
request.ContentType = "application/json; charset=utf-8";
string text;
try
{
var response = (HttpWebResponse) request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
uxResponse.Text = text;
OpenTSDBResponse myObject = (OpenTSDBResponse)Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(OpenTSDBResponse));
var variable = Newtonsoft.Json.JsonConvert.DeserializeObject(text);
//var tester = myObject;
}
catch (Exception ex)
{
uxResponse.Text = GetFullExceptionMessage(ex);
}
The JSON I am receiving from the above code (i.e. the 'text' variable):
[{
"metric":"tcollector.collector.lines_sent",
"tags":
{
"host":"ubuntu1"
},
"aggregateTags":["collector"],
"dps":
{
"1377050434":1271779.0,
"1377050494":1272073.0,
"1377050554":1272502.0,
"1377050614":1273632.0,
"1377050674":1273867.0
}
}]
My c# classes
internal class OpenTSDBResponse
{
[JsonProperty("metric")]
public string Metric { get; set; }
[JsonProperty("tags")]
public Tags Tags { get; set; }
[JsonProperty("aggregateTags")]
public string[] AggregateTags { get; set; }
[JsonProperty("dps")]
public List<TimeValue> TimeValues { get; set; }
}
internal class Tags
{
[JsonProperty("host")]
public string Host { get; set; }
}
internal class TimeValue
{
[JsonProperty("Time")]
public double Time { get; set; }
[JsonProperty("Value")]
public double Value { get; set; }
}
The error when deserializing object:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'MyNamespace.OpenTSDBResponse' 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.
Additional Information
I used the codeproject deserialize JSON project to create my basic classes, but it created a new c# property for each '"1377050434":1271779.0,' so I updated to use my TimeValue class. http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-C
Question:
How do I get this into an appropriate c# class structure?
Additional Information in response to users comments below:
bjaminn's comment:
I believe the JSON you are receiving is an array. The exception is trying to say you are converting an object[] to OpenTSDBResponse when you really want OpenTSDBResponse[]. Another way to debug this would be to look at the variable variable and see what type it is in the debugger. Of course the line that throws the exception would need to be commented out.
Outcome: I modified the deserialize like this
OpenTSDBResponse[] myObject = (OpenTSDBResponse[])Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(OpenTSDBResponse[]));
but received the following error when I ran it:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MyNamespace.OpenTSDBResponseJsonTypes.TimeValue]' 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 '[0].dps.1377050434', line 1, position 121.
Additional Notes on working solution for other new to JSON
I have added another property to my class for the Dictionary as it's really "unix-time-stamp-data","Value". This allows me to work in c# with datetime/values. There may be a better way for casting but this works an doesn't cause any noticeable performance issues for my scenario.
[JsonProperty("dps")]
public Dictionary<string, double> TimeValues { get; set; }
public List<TimeValue> DataPoints
{
get
{
List<TimeValue> times = new List<TimeValue>();
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
foreach (var item in TimeValues)
{
times.Add(new TimeValue
{
Time = dtDateTime.AddSeconds(double.Parse(item.Key)).ToLocalTime(),
Value = item.Value
});
}
return times;
}
}
I believe the JSON you are receiving is an array. The exception is trying to say you are converting an object[] to OpenTSDBResponse when you really want OpenTSDBResponse[].
Another way to debug this would be to look at the variable variable and see what type it is in the debugger. Of course the line that throws the exception would need to be commented out.
Tackling new error
It looks like DPS is not a proper JSON array. You could parse it to a dictionary since it looks like the keys will be different in each JSON call.
JSON convert dictionary to a list of key value pairs
New class:
internal class OpenTSDBResponse
{
[JsonProperty("metric")]
public string Metric { get; set; }
[JsonProperty("tags")]
public Tags Tags { get; set; }
[JsonProperty("aggregateTags")]
public string[] AggregateTags { get; set; }
[JsonProperty("dps")]
public Dictionary<string,double> TimeValues { get; set; }
}
You can such so modify your Json Data and your C# Code,for example
[{
"metric":"tcollector.collector.lines_sent",
"tags":
{
"host":"ubuntu1"
},
"aggregateTags":["collector"],
"dps":
[
{"Time":"1377050434","Value":1271779.0},
{"Time":"1377050554","Value":1272502.0}
]
}]
c# Code:
You provide the data is an Array,so when you deserialize the string,you must such so use generic format of deserializeobject
object obj=Newtonsoft.Json.JsonConvert.DeserializeObject<List<OpenTSDBResponse>>(json.ToString());

Categories

Resources