Invalidcastexception JsonConvert.DeserializeObject - c#

I am getting an invalid cast exception that the specified cast is not valid. On this line:
RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1);
From the documentation this should be fine? I can see the console output is fine?
Response: [{"Height_ft": 2999.0, "Height_m": 914.0, "ID": "c1",
"Latitude": 57.588007, "Longitude": -5.5233564, "Name": "Beinn Dearg",
"humidity": 0.81, "snowCover": 4.99, "temperature": 63.0}]
Spinner spinner = (Spinner)sender;
string urlmountain = "http://removed.azurewebsites.net/api/Mountains?name=";
JsonValue json1 = FetchMountain(urlmountain+string.Format("{0}", spinner.GetItemAtPosition(e.Position)));
//below.................................
RootObject mountain = JsonConvert.DeserializeObject<RootObject>(json1); //this line
string toast = mountain.Name;
Toast.MakeText(this, toast, ToastLength.Long).Show();
private JsonValue FetchMountain(string urlmountain)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlmountain));
request.ContentType = "application/json";
request.Method = "GET";
// Send the request to the server and wait for the response:
using (WebResponse response = request.GetResponse())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc1 = JsonObject.Load(stream);
Console.Out.WriteLine("Response: {0}", jsonDoc1.ToString());
// Return the JSON document:
return jsonDoc1;
}
}
}
public class RootObject
{
public string ID { get; set; }
public double? Latitude { get; set; }
public double? Longitude { get; set; }
public string Name { get; set; }
public double? Height_m { get; set; }
public double? Height_ft { get; set; }
public double? temperature { get; set; }
public double? humidity { get; set; }
public double? snowCover { get; set; }
public override string ToString()
{
return Name;
}
}

The json data being returned is an array of objects, not a single object, as denoted by the opening and closing brackets []. You need to deserialize to an array or a list:
var mountains = JsonConvert.DeserializeObject<List<RootObject>>(json);
To access the first mountain from the deserialized payload, use .FirstOrDefault().
var mountain = mountains.FirstOrDefault();
if (mountain != null)
{
string toast = mountain.Name;
Toast.MakeText(this, toast, ToastLength.Long).Show();
}

It looks like your JSON is an Array of objects. You should be able to deserialize the array and get the first one like so:
RootObject mountain = JsonConvert.DeserializeObject<RootObject[]>(json1)[0];
One thing to note is that you are sort of mixing technologies here. JsonValue is from the System.Json namespace, whereas JsonConvert is from the Newtonsoft.Json (i.e. JSON.Net) namespace. If you wanted to go strictly with JSON.Net, you could do something like this:
private RootObject FetchMountain(string urlmountain)
{
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlmountain));
request.ContentType = "application/json";
request.Method = "GET";
using (WebResponse response = request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader streamReader = new StreamReader(stream))
{
JsonSerializer serializer = new JsonSerializer();
RootObject[] mountains = (RootObject[])serializer.Deserialize(streamReader, typeof(RootObject[]));
return (mountains.Length > 0) ? mountains[0] : null;
}
}

Related

How to pass incoming integer parameter value in json request while making a POST request

Making a POST request for the API, passing incoming json body as hardcoded string. Fine with all hardcoded values, only need to pass ID based on incoming parameter incomingID (int)
Checked couple of articles/questions, but didn't any clear answer. Please suggest/guide how to replace this hardcoded value 123456 with incoming parameter value (incomingID)
Replace field in Json file with some other value
How to replace placeholders inside json string?
private void CallAPI(int incomingID)
{
const string apiURL = "API URL"; // some test API
string getInfoResult = string.Empty;
try
{
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(apiURL);
webrequest.Method = "POST";
webrequest.ContentType = "application/json";
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
string json = "{\"Information\":{\"messageHeader\":{\"message\":\"getInfo\",\"transactionDate\":\"2021-05-11T12:05:54.000\", \"transactionID\":\"2021-05-15T12:05:54.000-12345\",\"payLoadFormat\":\"V1\",\"ID\":123456}}}"; //pass this incomingID based on parameter value
streamWriter.Write(json);
}
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), enc);
getInfoResult = responseStream.ReadToEnd();
webresponse.Close();
}
catch (Exception ex)
{
throw ex;
}
}
To make this, you can use simple interpolation:
string json = $"{{\"Information\":{{\"messageHeader\":{{\"message\":\"getInfo\",\"transactionDate\":\"2021-05-11T12:05:54.000\", \"transactionID\":\"2021-05-15T12:05:54.000-12345\",\"payLoadFormat\":\"V1\",\"ID\":{incomingID}}}}}}}"
Note that open and close brackets should be repeated twice to escape them. But, this is weird and not so common way to make JSON strings. Better use serialization to achieve your goal. For an example, use Newtonsoft.Json nuget to (de)serialize your objects. What you need to do:
Create your models as separate classes:
PayloadFormat.cs
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))] // this string will force your enum serialize as "V1" instead of "0"
public enum PayloadFormat
{
V1,
V2,
V3
}
MessageHeader.cs
public class MessageHeader
{
public MessageHeader(string message, DateTime transactionDate, string transactionId, PayloadFormat payloadFormat, int id)
{
Message = message;
TransactionDate = transactionDate;
TransactionId = transactionId;
PayloadFormat = payloadFormat;
Id = id;
}
public string Message { get; set; }
public DateTime TransactionDate { get; set; } // change type if you need to
public string TransactionId { get; set; } // change type if you need to
public PayloadFormat PayloadFormat { get; set; } // change type if you need to
public int Id { get; set; }
}
Information.cs
public class Information
{
public Information(MessageHeader messageHeader)
{
MessageHeader = messageHeader;
}
public MessageHeader MessageHeader { get; set; }
}
Create an instance of your Information class:
var information = new Information(new MessageHeader("getInfo", DateTime.Now, $"{DateTime.Now}-12345", PayloadFormat.V1, incomingID));
Serialize your string (make sure you are using Newtonsoft.Json;):
var json = JsonConvert.SerializeObject(information);
Then use your json as you need to. The result will be:
{
"MessageHeader": {
"Message": "getInfo",
"TransactionDate": "2022-05-17T19:45:33.2161326+05:00",
"TransactionId": "17.05.2022 19:45:33-12345",
"PayloadFormat": "V1",
"Id": 5
}
}

Receive API Response in c#

im trying to get a this value that i send with JSON string POST, when i receive the response in postman, there's value like status, id, etc that i not POST, while i do understand get the value from JSON string,
i dont quite understand to get a receive API value like status and Name
what i do tried recently
public class Condition
{
public string status { get; set; }
public string name { get; set; }
public string positition { get; set; }
public int no_id { get; set; }
public string time_auth { get; set; }
public string account_type { get; set; }
}
for the method
public partial class ResponseJSON
{
public bool Result(string jsonData, string URL, out string status)
{
bool resultresponse = false;
var request = (HttpWebRequest)WebRequest.Create(URL);
request.ContentType = "application/json";
request.Method = "POST";
var writer = new StreamWriter(request.GetRequestStream());
string wrote = jsonData;
writer.Write(wrote);
var httpResponse = (HttpWebResponse)request.GetResponse();
var streamReader = new StreamReader(httpResponse.GetResponseStream());
var result = streamReader.ReadToEnd();
dynamic R = JsonConvert.DeserializeObject<dynamic>(wrote);
status = R.auth_type;
return resultresponse;
}
}
and for the main
string jsonData = #"{
'auth_type':'7',
'staff_id':'1234567890',
'staff_pin': '1234'}";
dynamic data = JObject.Parse(jsonData);
string URL = "Link URL";
string status = string.Empty;
ResponseJSON responseJSON = new ResponseJSON();
responseJSON.Result(jsonData, URL, out status);
You might want to declare the same class "Condition" on your client side, so you can deserialize the response with Newtonsoft with something like this:
var conditionResponse = JsonConvert.DeserializeObject<Condition>(jsonData);

Parsing uniquely named nested JSON

Preface: I know of JSON.NET but I cannot use it (client machine).
I need to parse the JSON returned by http://api.fixer.io/latest?base=USD into 3 columns, date, currencyCode, and rate. The issue is with the nested "rates" portion. The currency code is the name of the first element which means I can't use "key" and "value" properties. The only way I know of is to hardcode each possible currency code, which is what I have currently in the code below. I want to be able to use key/value pairs to pull the code/rate simultaneously.
The JSON:
{"base":"USD",
"date":"2016-07-12",
"rates": {
"AUD":1.3101,
"BGN":1.7633,
"BRL":3.2829,
"CAD":1.3029,
etc....}
}
My code so far:
static void Main(string[] args)
{
var curDate = "2001-01-01";
var URL = #"http://api.fixer.io/" + curDate + "?base=USD";
Console.WriteLine(URL);
//WebRequest wrGetURL = WebRequest.Create(URL);
var text = "";
//wrGetURL.ContentType = "application/json; charset=utf-8";
HttpWebRequest httpWebRequest = System.Net.WebRequest.Create(URL) as HttpWebRequest;
using (HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse)
{
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{
throw new Exception(string.Format("Server error (HTTP {0}: {1}).",
httpWebResponse.StatusCode, httpWebResponse.StatusDescription));
}
Stream stream = httpWebResponse.GetResponseStream();
DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(JSONRead));
JSONRead objResponse = (JSONRead)dataContractJsonSerializer.ReadObject(stream);
Console.WriteLine(objResponse.rates.AUD);
}
Console.ReadLine();
}
[DataContract]
public class JSONRead
{
[DataMember(Name = "date")]
public string date { get; set; }
[DataMember(Name = "rates")]
public Rates rates { get; set; }
[DataMember(Name = "base")]
public string bases { get; set; }
}
[DataContract]
public class Rates
{
[DataMember(Name = "AUD")]
public string AUD { get; set; }
//[DataMember(Name = "key")]
//public string key { get; set; }
//[DataMember(Name = "value")]
//public string value { get; set; }
}
What I am trying to return:
Date Code Rate
2016-07-12 AUD 1.3101
2016-07-12 GBN 1.7633
etc...
I had to use the DataContractJsonSerializerSettings and set UseSimpleDictionaryFormat to true. It then reads the nested object into a Dictionary object properly. Thanks for the help #Plutonix.

Cannot Deserilize Json Format

I cannot deserilize this Json result
I have this code for that
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
using (TextReader reader = new StreamReader(responseStream, true))
{
valor = reader.ReadToEnd();
}
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(valor)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<ApiData>));
List<ApiData> obj = (List<ApiData>)serializer.ReadObject(stream);
}
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
}
throw;
}
But the result obj is null
ApiData Example
[DataContract]
public class ApiData
{
[DataMember(Order = 1)]
public string coArt { get; set; }
[DataMember(Order = 2)]
public string artDes { get; set; }
[DataMember(Order = 3)]
public string fechaReg { get; set; }
[DataMember(Order = 4)]
public bool manjSer { get; set; }
[DataMember(Order = 5)]
}
Anyone can help me or tell me whats wrong with the code or format casting? thanks you
Your root JSON entity isn't a list, it's an object with name/value pairs, the first name being 0 and the values being instances of your ApiData class. To deserialize this, you should use a Dictionary<string, ApiData> (or Dictionary<int, ApiData> if you are sure that only numbers appear as keys).
Thus:
var serializer = new DataContractJsonSerializer(typeof(Dictionary<string, ApiData>), new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });
var dict = (Dictionary<string, ApiData>)serializer.ReadObject(stream);
Note that you must set DataContractJsonSerializerSettings.UseSimpleDictionaryFormat = true to use DataContractJsonSerializer to parse dictionaries in this format -- and this setting is only available starting in .Net 4.5.

Error Json deserialization and List<>

I'm trying to deserialize some JSON response. With simple response I've no problem but with a complex response I get this error:
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[APIEffilogics.Usuari+Node]' 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.
It seems that I have a problem with the type of I put when deserialize the string. The JSON string is like this:
{
nodes: [
{
id: 5,
global_id: 5,
description: "Oven",
room_id: 2,
floor_id: 1,
building_id: 1,
client_id: 2,
nodemodel_id: 2,
nodetype_id: 1
},
{
id: 39,
global_id: 39,
description: "Fridge",
room_id: 2,
floor_id: 1,
building_id: 1,
client_id: 2,
nodemodel_id: 8,
nodetype_id: 1
}, ...
],
limit: 10,
offset: 0
}
And those are the classes:
public class Node : Usuari //Estructura nodes
{
[JsonProperty("limit")]
public int limit { get; set; }
[JsonProperty("offset")]
public int offset { get; set; }
[JsonProperty("nodes")]
public List<Node_sub> nodes_sub { get; set; }
}
public class Node_sub : Node
{
[JsonProperty("id")]
public string nid { get; set; }
[JsonProperty("global_id")]
public string gid { get; set; }
[JsonProperty("description")]
public string descrip { get; set; }
[JsonProperty("room_id")]
public string rid { get; set; }
[JsonProperty("floor_id")]
public string fid { get; set; }
[JsonProperty("client_id")]
public string cid { get; set; }
[JsonProperty("building_id")]
public string bid { get; set; }
[JsonProperty("nodemodel_id")]
public string model { get; set; }
[JsonProperty("nodetype_id")]
public string type { get; set; }
}
The code is:
public void Request(string url, string metode, string value)
{
try
{
//Enviem la petició a la URL especificada i configurem el tipus de connexió
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.KeepAlive = true;
myReq.Headers.Set("Cache-Control", "no-store");
myReq.Headers.Set("Pragma", "no-cache");
myReq.Headers.Set("Authorization", usuari.token_type + " " + usuari.access_token);
myReq.ContentType = "application/json";
if (metode.Equals("GET") || metode.Equals("POST"))
{
myReq.Method = metode; // Set the Method property of the request to POST or GET.
if (body == true)
{
// add request body with chat search filters
List<paramet> p = new List<paramet>();
paramet p1 = new paramet();
p1.value = "1";
string jsonBody = JsonConvert.SerializeObject(value);
var requestBody = Encoding.UTF8.GetBytes(jsonBody);
myReq.ContentLength = requestBody.Length;
//myReq.ContentType = "application/json";
using (var stream = myReq.GetRequestStream())
{
stream.Write(requestBody, 0, requestBody.Length); //Enviem el cos de la petició
}
body = false;
}
}
else throw new Exception("Invalid Method Type");
//Obtenim la resposta del servidor
HttpWebResponse myResponse = (HttpWebResponse)myReq.GetResponse();
Stream rebut = myResponse.GetResponseStream();
StreamReader readStream = new StreamReader(rebut, Encoding.UTF8); // Pipes the stream to a higher level stream reader with the required encoding format.
string info = readStream.ReadToEnd();
if (tipus == 0) jsonclient = JsonConvert.DeserializeObject<List<Usuari.Client>>(info);
else if (tipus == 1) jsonedif = JsonConvert.DeserializeObject<List<Usuari.Building>>(info);
else if (tipus == 2) jsonplanta = JsonConvert.DeserializeObject<List<Usuari.Floor>>(info);
else if (tipus == 3) jsonhab = JsonConvert.DeserializeObject<List<Usuari.Room>>(info);
else if (tipus == 4) jsonnode = JsonConvert.DeserializeObject<List<Usuari.Node>>(info);
}
catch (WebException ex)
{
// same as normal response, get error response
var errorResponse = (HttpWebResponse)ex.Response;
string errorResponseJson;
var statusCode = errorResponse.StatusCode;
var errorIdFromHeader = errorResponse.GetResponseHeader("Error-Id");
using (var responseStream = new StreamReader(errorResponse.GetResponseStream()))
{
errorResponseJson = responseStream.ReadToEnd();
}
//var errorCode = JsonObject.Parse(errorResponseJson).Object("responseStatus")["errorCode"];
//var errorMessage = JsonObject.Parse(errorResponseJson).Object("responseStatus")["message"];
}
}
Why I'm having this error? List<Usuari.Node> is an array that contains all the items of JSON message. I try to fix the error but I'm not able to find and answer. How can I fix it?
Thanks
The service returns just one Node, instead of an array of Nodes with one element. To solve this problem, you can use one of these approaches:
Change the service so it always returns an array (by wrapping the result before returning it).
If 1. is not an option: Differentiate the different response types in the client (check if the response is an array and if not, tell the serializer to parse a single Node instead of a list) and either handle the single object differently or just wrap it in a list and then proceed as if the server returned it like that.

Categories

Resources